博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linq练习
阅读量:6912 次
发布时间:2019-06-27

本文共 2643 字,大约阅读时间需要 8 分钟。

在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
 ids=new List
{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()返回的是符合条件的第一行

转载于:https://www.cnblogs.com/fjsnail/p/3229507.html

你可能感兴趣的文章
selinux开启关闭
查看>>
linux 编译ffmpeg 支持x264, x265
查看>>
输入子系统--event层分析【转】
查看>>
fragment生命周期
查看>>
在Windows Server 2012 中安装 .NET 3.5 Framework
查看>>
git 笔记
查看>>
Sphinx学习之sphinx的安装篇
查看>>
微软Power BI技术文章与资源目录
查看>>
WWDC2016-session402-whatsNewInSwift3
查看>>
Android 采用post方式提交数据到服务器
查看>>
测试性分析
查看>>
Spring MVC @RequestParam
查看>>
python+Eclipse+pydev环境搭建
查看>>
ASP.NET MVC案例——————拦截器
查看>>
javascript垃圾回收
查看>>
【Python】TF环境
查看>>
Apache下的子目录以及相关指令的说明
查看>>
jqury 插件
查看>>
通过计算机名访问linux
查看>>
模式识别开发之项目---计算机视觉目标检测的框架与过程
查看>>