1、查询Student表中的所有记录的Sname、Ssex和Class列。

SQL:select sname,ssex,class from Students

linq: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列。

SQL:select distinct depart from Teachers

linq:from t in Teachers.distinct() select t.depart

lambda:Teachers.distinct().select(t=>t.depart)

3、查询Student表的所有记录。

SQL:select * from students

linq:from s in students select s

lambda:students.select(s=>s)

4、查询Score表中成绩在60到80之间的所有记录。

SQL:select * from Score where degree between 60 and 80

linq:from s in Score where s.degree>=60 && s.degree<=80 select s

lambda:Score.where(s=>(s.degree>=60 && s.degree<=80))

5、查询Score表中成绩为85,86或88的记录。

SQL:select * from Score where degree in (85,86,88)

linq:from s in Score where (new decimal[]{85,86,88}).contains(s.degree) select s

lambda:Score.where(s=>(new Decimal[]{85,86,88}.contains(s.degree)))

查询Score表中成绩不是85,86或88的记录。

SQL:select * from Score where degree not in (85,86,88)

linq:from s in Score where !(new decimal[]{85,86,88}).contains(s.degree) select s

lambda:Score.where(s=>!(new Decimal[]{85,86,88}.contains(s.degree)))

6、查询Student表中"95031"班或性别为"女"的同学记录。

SQL:select * from students where class ='95031' or ssex= N'女'

linq:from s in Students where s.class="95031" || s.ssex="女" select s

lambda:Students.where(s=>(s.class="95031"||s.ssex="女"))

7、以Class降序查询Student表的所有记录。

SQL:select * from students order by Class DESC

linq:from s in students orderby s.class Descending select s

lambda:Students.OrderByDescending(s=>s.class)

8、以Cno升序、Degree降序查询Score表的所有记录。

SQL:select * from score order by Cno ASC,Degree DESC

linq:from s in Score orderby s.degree descending orderby s.cno ascending select s

lambda:Score.OrderByDescending(s=>s.degree).OrderBy(s=>s.cno)

9、查询"95031"班的学生人数。

SQL:select count(*) from students 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表中的最高分的学生学号和课程号。

SQL:select distinct s.Sno,c.Cno from students 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()

11、查询'3-105'号课程的平均分。

SQL: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(*)>=5

linq: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))

13、查询最低分大于70,最高分小于90的Sno列。

select sno from score group by sno having min(degree) > 70 and max(degree) < 90

linq: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.sno

linq: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.name,cno=sc.cno,degree=sc.degree})

15、查询所有学生的Sno、Cname和Degree列。

SQL:select sc.sno,c.cname,sc.degree from course as c,score as sc where c.cno = sc.cno

linq:from c in Courses join sc in Scores on c.cno equals sc.cno select new {sc.sno,c.came,sc.degree}

lambda:Courses.join(Scores,c=>c.no,sc=>sc.cno,(c,sc)=>new{sno=sc.sno,cname=c.cname,degree=sc.degree})

16、查询所有学生的Sname、Cname和Degree列。

SQL: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.cno

linq: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}

SQL,Linq,Lambda之间的转换练习的更多相关文章

  1. SQL,LINQ,Lambda语法对照图(转载)

    如果你熟悉SQL语句,当使用LINQ时,会有似曾相识的感觉.但又略有不同.下面是SQL和LINQ,Lambda语法对照图 SQL LINQ Lambda SELECT * FROM HumanReso ...

  2. C# ORM中Dto Linq Expression 和 数据库Model Linq Expression之间的转换

    今天在百度知道中看到一个问题,研究了一会便回答了: http://zhidao.baidu.com/question/920461189016484459.html 如何使dto linq 表达式转换 ...

  3. sql linq lambda 对比

    . 查询Student表中的所有记录的Sname.Ssex和Class列. select sname,ssex,class from student Linq: from s in Students ...

  4. 构造复杂Lambda困惑之学会用LinqPad和Linqer实现Sql 和 Lambda之间的互转

    一:linq的话我们可能会遇到两个问题: 1. 我们的linq出现性能低下的时候,如果优化???? 我们写的linq所生成的sql是无法控制的... (要做性能优化,必须预先知道sql会生成啥样的?? ...

  5. SQL/LINQ/Lamda 写法[转发]

    SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees   .Sele ...

  6. SQL Linq lamda区别

    SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees   .Sele ...

  7. SQL/LINQ/Lamda

    SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employees select e Employees   .Sele ...

  8. linq中查询列表的使用及iqueryable和list集合之间的转换

    linq中查询列表的使用及iqueryable和list集合之间的转换 比如要查询一个货架集合,但是只需要其id和name即可,可以用以下方法:先写一个model类:CatalogModel(注意该类 ...

  9. java.util.Date 与 java.sql.Date 之间的转换

    SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); String dateStr = sdf.for ...

随机推荐

  1. C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 所有的基础数据都可以恢复删除

    客户的需求如下: 所有基礎信息需要記錄創建人,創建時間,更改人,更改時間,刪除人,刪除時間.有停用基礎信息功能(停用不是刪除,只是暫時停用).基礎信息可以查出已經刪除的信息(有選項可以選擇),有方法把 ...

  2. Linux Linux程序练习十八

    题目:编写一个TCP服务器和客户端,基于TCP的并发远程shell 要求实现: )对于所有收到的客户端消息,作为命令行进行执行, 并且将命令行的输出结果返回给客户端 )要求使用并发结构 )实现关键代码 ...

  3. Golang友团无闻Go语言Web基础视频教程

    教程内容:GO语言资料Golang友团无闻Go语言编程基础Golang友团无闻Go语言Web基础教程 Go语言Web基础教程列表:[Go Web基础]12Go Web 扩展学习.mp4[Go Web基 ...

  4. 几种display:table-cell的应用

    一.display:table-cell属性简述 display:table- cell属性指让标签元素以表格单元格的形式呈现,类似于td标签.目前IE8+以及其他现代浏览器都是支持此属性的,但是IE ...

  5. C#和C实现通过CRC-16 (Modbus)获取CRC值并校验数据(代码)

    文章首发于浩瀚先森博客 CRC的全称为Cyclic Redundancy Check,中文名称为循环冗余校验.它是一类重要的线性分组码,编码和解码方法简单,检错和纠错能力强,在通信领域广泛地用于实现差 ...

  6. end2end learning 端到端学习

    在DeepLearning的文章中有看到end2end一次,作者们似乎都比较喜欢这个end2end learning的方式.那么到底啥是end2end? 找了一下相关论文,没找到专门讲这个概念的,看来 ...

  7. bzoj2064[和谐社会模拟赛]分裂

    题意:给定一个初始集合和目标集合,有两种操作:1.合并集合中的两个元素,新元素为两个元素之和 2.分裂集合中的一个元素,得到的两个新元素之和等于原先的元素.要求用最小步数使初始集合变为目标集合,求最小 ...

  8. 基于Twitter-Snowflake的java改进版,去状态化实现

    package jeffery; import java.net.InetAddress; import java.net.UnknownHostException; import java.util ...

  9. 搭建maven项目

    一.新建项目 点击"Maven Project",如果没有的话在"Other"里面 勾选这个选项,这样可以建立一个没有多余文件的简单项目(如果这个时候直接建立w ...

  10. 查找树ADT——二叉搜索树

    在以下讨论中,虽然任意复杂的关键字都是允许的,但为了简单起见,假设它们都是整数,并且所有的关键字是互异的. 总概   使二叉树成为二叉查找树的性质是,对于树中的每个节点X,它的左子树中所有关键字值小于 ...