博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL、LINQ、Lambda 三种用法(转)
阅读量:6383 次
发布时间:2019-06-23

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

SQL、LINQ、Lambda 三种用法颜色注释: SQL LinqToSql Lambda QA1、 查询Student表中的所有记录的Sname、Ssex和Class列。select sname,ssex,class from studentLinq:    from s in Students    select new {        s.SNAME,        s.SSEX,        s.CLASS    }Lambda:    Students.Select( s => new {        SNAME = s.SNAME,SSEX = s.SSEX,CLASS = s.CLASS    })2、 查询教师所有的单位即不重复的Depart列。select distinct depart from teacherLinq:    from t in Teachers.Distinct()    select t.DEPARTLambda:    Teachers.Distinct().Select( t => t.DEPART)3、 查询Student表的所有记录。select * from studentLinq:    from s in Students    select sLambda:    Students.Select( s => s)4、 查询Score表中成绩在60到80之间的所有记录。select * from score where degree between 60 and 80Linq:    from s in Scores    where s.DEGREE >= 60 && s.DEGREE < 80    select sLambda:    Scores.Where(         s => (                s.DEGREE >= 60 && s.DEGREE < 80              )    )5、 查询Score表中成绩为85,86或88的记录。select * from score where degree in (85,86,88)Linq:In    from s in Scores    where (            new decimal[]{85,86,88}          ).Contains(s.DEGREE)    select sLambda:    Scores.Where( s => new Decimal[] {85,86,88}.Contains(s.DEGREE))Not in    from s in Scores    where !(            new decimal[]{85,86,88}          ).Contains(s.DEGREE)    select sLambda:    Scores.Where( s => !(new Decimal[]{85,86,88}.Contains(s.DEGREE)))    Any()应用:双表进行Any时,必须是主键为(String)    CustomerDemographics CustomerTypeID(String)    CustomerCustomerDemos (CustomerID CustomerTypeID) (String)    一个主键与二个主建进行Any(或者是一对一关键进行Any)    不可,以二个主键于与一个主键进行Any        from e in CustomerDemographics    where !e.CustomerCustomerDemos.Any()    select e        from c in Categories    where !c.Products.Any()    select c6、 查询Student表中"95031"班或性别为"女"的同学记录。select * from student where class ='95031' or ssex= N'女'Linq:    from s in Students    where s.CLASS == "95031"        || s.CLASS == "女"    select sLambda:    Students.Where(s => ( s.CLASS == "95031" || s.CLASS == "女"))7、 以Class降序查询Student表的所有记录。select * from student order by Class DESCLinq:    from s in Students    orderby s.CLASS descending    select sLambda:    Students.OrderByDescending(s => s.CLASS)8、 以Cno升序、Degree降序查询Score表的所有记录。select * from score order by Cno ASC,Degree DESCLinq:(这里Cno ASC在linq中要写在最外面)    from s in Scores    orderby s.DEGREE descending    orderby s.CNO ascending     select sLambda:    Scores.OrderByDescending( s => s.DEGREE)          .OrderBy( s => s.CNO)9、 查询"95031"班的学生人数。select count(*) from student where class = '95031'Linq:    (    from s in Students        where s.CLASS == "95031"        select s    ).Count()Lambda:    Students.Where( s => s.CLASS == "95031" )                .Select( s => s)                    .Count()10、查询Score表中的最高分的学生学号和课程号。select distinct s.Sno,c.Cno from student as s,course as c ,score as sc where s.sno=(select sno from score where degree = (select max(degree) from score))and c.cno = (select cno from score where degree = (select max(degree) from score))Linq:    (        from s in Students        from c in Courses        from sc in Scores        let maxDegree = (from sss in Scores                        select sss.DEGREE                        ).Max()        let sno = (from ss in Scores                where ss.DEGREE == maxDegree                select ss.SNO).Single().ToString()        let cno = (from ssss in Scores                where ssss.DEGREE == maxDegree                select ssss.CNO).Single().ToString()        where s.SNO == sno && c.CNO == cno        select new {            s.SNO,            c.CNO        }    ).Distinct()操作时问题?执行时报错: where s.SNO == sno(这行报出来的) 运算符"=="无法应用于"string"和"System.Linq.IQueryable
"类型的操作数解决:原:let sno = (from ss in Scores where ss.DEGREE == maxDegree select ss.SNO).ToString()Queryable().Single()返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。 解:let sno = (from ss in Scores where ss.DEGREE == maxDegree select ss.SNO).Single().ToString()11、查询'3-105'号课程的平均分。select avg(degree) from score where cno = '3-105'Linq: ( from s in Scores where s.CNO == "3-105" select s.DEGREE ).Average()Lambda: Scores.Where( s => s.CNO == "3-105") .Select( s => s.DEGREE) .Average()12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。select avg(degree) from score where cno like '3%' group by Cno having count(*)>=5Linq: from s in Scores where s.CNO.StartsWith("3") group s by s.CNO into cc where cc.Count() >= 5 select cc.Average( c => c.DEGREE)Lambda: Scores.Where( s => s.CNO.StartsWith("3") ) .GroupBy( s => s.CNO ) .Where( cc => ( cc.Count() >= 5) ) .Select( cc => cc.Average( c => c.DEGREE) )Linq: SqlMethodlike也可以这样写: s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")13、查询最低分大于70,最高分小于90的Sno列。select sno from score group by sno having min(degree) > 70 and max(degree) < 90Linq: from s in Scores group s by s.SNO into ss where ss.Min(cc => cc.DEGREE) > 70 && ss.Max( cc => cc.DEGREE) < 90 select new { sno = ss.Key }Lambda: Scores.GroupBy (s => s.SNO) .Where (ss => ((ss.Min (cc => cc.DEGREE) > 70) && (ss.Max (cc => cc.DEGREE) < 90))) .Select ( ss => new { sno = ss.Key })14、查询所有学生的Sname、Cno和Degree列。select s.sname,sc.cno,sc.degree from student as s,score as sc where s.sno = sc.snoLinq: from s in Students join sc in Scores on s.SNO equals sc.SNO select new { s.SNAME, sc.CNO, sc.DEGREE }Lambda: Students.Join(Scores, s => s.SNO, sc => sc.SNO, (s,sc) => new{ SNAME = s.SNAME, CNO = sc.CNO, DEGREE = sc.DEGREE })15、查询所有学生的Sno、Cname和Degree列。select sc.sno,c.cname,sc.degree from course as c,score as sc where c.cno = sc.cnoLinq: from c in Courses join sc in Scores on c.CNO equals sc.CNO select new { sc.SNO,c.CNAME,sc.DEGREE }Lambda: Courses.Join ( Scores, c => c.CNO, sc => sc.CNO, (c, sc) => new { SNO = sc.SNO, CNAME = c.CNAME, DEGREE = sc.DEGREE })16、查询所有学生的Sname、Cname和Degree列。select s.sname,c.cname,sc.degree from student as s,course as c,score as sc where s.sno = sc.sno and c.cno = sc.cnoLinq: from s in Students from c in Courses from sc in Scores where s.SNO == sc.SNO && c.CNO == sc.CNO select new { s.SNAME,c.CNAME,sc.DEGREE }

 

转载地址:http://ogwha.baihongyu.com/

你可能感兴趣的文章
vim:去掉响铃
查看>>
Spring 小示例
查看>>
MySql清空表的方法介绍 : truncate table 表名
查看>>
codeforces水题100道 第四题 Codeforces Round #105 (Div. 2) A. Insomnia cure (math)
查看>>
利用 SPL 快速实现 Observer 设计模式: SplSubject 、SplObserver与SplObjectStorage【转】
查看>>
C\C++ 1A2B小游戏源码
查看>>
【SDK fix】iOS 8下将UIButton放置于tabbar位置无法响应event
查看>>
Android项目实战(三十八):2017最新 将AndroidLibrary提交到JCenter仓库(图文教程)...
查看>>
地平线“小目标”:2025年,三千万汽车搭载地平线自动驾驶BPU
查看>>
“2016大数据技术与应用人才培养研讨会” 在泸州成功召开
查看>>
大数据和数字化转型
查看>>
如何知道自己的CPU支持SLAT
查看>>
客户端在使用citrix应用如何开启本地输入法
查看>>
C# 一个字符串是否在另外一个字符串数组里 Array.Exists 的用法 Array.IndexOf 用法...
查看>>
delphi实现计算器
查看>>
CentOS7 网卡命名
查看>>
Django进阶之缓存和信号
查看>>
DataGridView 设定单元格只读:
查看>>
缺陷跟踪工具jira和团队协作与项目管理工具conflunce
查看>>
shell特性及变量设置
查看>>