• linq练习


    在csdn上找一些问题,自己研究了一会

    1.练习一

    select id,name from table order by case when id like 'shanghai%' then 0 when id like 'beijing%' then 1 else 2 end 这条sql 能转化成Linq吗

    答案:

     var result1 = from item in study.Customer
                              orderby item.ContactName.Contains("j") ? 0 : item.Country.Contains("t") ? 1 : 2 descending
                             // orderby item.CustomerID  descending
                             select new { item.CustomerID, item.Country, item.ContactName };
     
                var result = study.Customer.OrderByDescending(n =>n.ContactName.Contains("j")?1:n.ContactName.Contains("T")?2:3).Select(n => new { n.CustomerID, n.Country, n.ContactName });
                foreach(var singel in result1)
                {
                    Console.WriteLine(singel);
                }

    2.练习二 select * from student where [id] in (1,2,3,4)怎么写成linq

    答案:这里我用的是EDM数据

       List<int> ids=new List<int>{1,2,3,4};
                var result4 = from item in study.Customer
                              where ids.Contains(item.CustomerID)
                              select item;
     
                var result44 = study.Customer.Where(n=>new int[]{1,2,3,4}.Contains(n.CustomerID));
     
                var result5 = from item in study.Customer
                              where new int[] { 1, 2, 3, 4 }.Contains(item.CustomerID)  //匿名数组,也是一个数据集
                              select item;
              
                var result6 = from item in study.Customer
                              join single in ids
                              on item.CustomerID equals single   //ids是一个数组,直接equals就行,他会取出数一个个比较
                              select item;
                var resutl7 = from item in study.Customer
                              where item.CustomerID >= 1 && item.CustomerID <= 4
                              select item;
                var result8 = from item in study.Customer
                              where ids.Any(m=>m==item.CustomerID)//ids也是一个数据集,m相当于遍历数据集中的每个元素
                              select item;
     
                var result88 = study.Customer.Where(n =>new int[]{1,2,3}.Any(m=>m==n.CustomerID));

    练习三
    有一个Module(ModuleId, ModuleName, ModuleActions,Right)数组,其中ModuleActions属性是一个ModuleAction(ActionId,ActionName,IsDefault)的数组。
    请用Linq语句查询出Module数组中Right为真的,ModuleAction的IsDefault为真的第一个ModuleAction组成的一个新的对象数组,
    叫做ModuleModle(ModuleId ,ModuleName,ActionName)。
    答案:
    var query=db.Module.Where(x=>x.Right==true&&x.ModuleActions.IsDefault==true)
    .Select(y=>new {y.ModuleId,y.ModuleName,y.ModuleAction.ActionName }).FirstOrDefault().ToArray();
    解释:x=>x.Right==true&&x.ModuleActions.IsDefault==true 因为这里两张表时有主外键的关系,通过外键moduleAction可以去到另一张表的isdefault
    firstDefault()返回的是符合条件的第一行
  • 相关阅读:
    BZOJ3697: 采药人的路径
    解题:WC 2007 石头剪刀布
    解题:CQOI 2017 老C的方块
    解题:洛谷4314 CPU监控
    解题:CQOI 2017 老C的任务
    解题:CF1009 Dominant Indices
    解题:CF570D Tree Requests
    解题:APIO 2012 派遣
    解题:ZJOI 2015 幻想乡战略游戏
    解题:洛谷4178 Tree
  • 原文地址:https://www.cnblogs.com/fjsnail/p/3229507.html
Copyright © 2020-2023  润新知