EF中的三种linq、lambda、sql查询方式

sql 、linq、lambda 查询语句的区别


 LINQ的书写格式如下:
from 临时变量 in 集合对象或数据库对象
where 条件表达式
   [order by
条件]
   select
临时变量中被查询的值
[group by 条件]

Lambda
表达式的书写格式如下:
(
参数列表) => 表达式或者语句块
其中:参数个数:可以有多个参数,一个参数,或者无参数。
参数类型:可以隐式或者显式定义。

表达式或者语句块:这部分就是我们平常写函数的实现部分(函数体)。 



1.查询全部

[sql] view plain copy

1.  查询Student表的所有记录。  

2.  select * from student  

3.  Linq:  

4.      from s in Students  

5.      select s  

6.  Lambda:  

7.      Students.Select( s => s)  



2 按条件查询全部:

查询Student表中的所有记录的SnameSsexClass列。

select sname,ssex,class from student

[sql] view plain copy

1.  Linq:  

2.      from s in Students  

3.      select new {  

4.          s.SNAME,  

5.          s.SSEX,  

6.          s.CLASS  

7.      }  

8.  Lambda:  

9.      Students.Select( s => new {  

10.          SNAME = s.SNAME,SSEX = s.SSEX,CLASS = s.CLASS  

11.      })  



3.distinct 去掉重复的

[sql] view plain copy

1.  查询教师所有的单位即不重复的Depart列。  

2.  select distinct depart from teacher  

3.  Linq:  

4.      from t in Teachers.Distinct()  

5.      select t.DEPART  

6.  Lambda:  

7.      Teachers.Distinct().Select( t => t.DEPART)  



4.连接查询 between and

[sql] view plain copy

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

2.  select * from score where degree between 60 and 80  

3.  Linq:  

4.      from s in Scores  

5.      where s.DEGREE >= 60 && s.DEGREE < 80  

6.      select s  

7.  Lambda:  

8.      Scores.Where(  

9.          s => (  

10.                  s.DEGREE >= 60 && s.DEGREE < 80  

11.               )  

12.      )  



5.在范围内筛选 In

[sql] view plain copy

1.  select * from score where degree in (85,86,88)  

2.  Linq:  

3.      from s in Scores  

4.      where (  

5.              new decimal[]{85,86,88}  

6.            ).Contains(s.DEGREE)  

7.      select s  

8.  Lambda:  

9.      Scores.Where( s => new Decimal[] {85,86,88}.Contains(s.DEGREE))  



6.or 条件过滤

[sql] view plain copy

1.  查询Student表中"95031"班或性别为""的同学记录。  

2.  select * from student where class ='95031' or ssex= N''  

3.  Linq:  

4.      from s in Students  

5.      where s.CLASS == "95031"  

6.         || s.CLASS == ""  

7.      select s  

8.  Lambda:  

9.      Students.Where(s => ( s.CLASS == "95031" || s.CLASS == ""))  



7.排序

[sql] view plain copy

1.  以Class降序查询Student表的所有记录。  

2.  select * from student order by Class DESC  

3.  Linq:  

4.      from s in Students  

5.      orderby s.CLASS descending  

6.      select s  

7.  Lambda:  

8.      Students.OrderByDescending(s => s.CLASS)  


8.count
()行数查询

[sql] view plain copy

1.  select count(*) from student where class = '95031'  

2.  Linq:  

3.      (    from s in Students  

4.          where s.CLASS == "95031"  

5.          select s  

6.      ).Count()  

7.  Lambda:  

8.      Students.Where( s => s.CLASS == "95031" )  

9.                  .Select( s => s)  

10.                      .Count()  



10.avg()
平均

[sql] view plain copy

1.  查询'3-105'号课程的平均分。  

2.  select avg(degree) from score where cno = '3-105'  

3.  Linq:  

4.      (  

5.          from s in Scores  

6.          where s.CNO == "3-105"  

7.          select s.DEGREE  

8.      ).Average()  

9.  Lambda:  

10.      Scores.Where( s => s.CNO == "3-105")  

11.              .Select( s => s.DEGREE)  


11.
子查询

[sql] view plain copy

1.  查询Score表中的最高分的学生学号和课程号。  

2.  select distinct s.Sno,c.Cno from student as s,course as c ,score as sc  

3.  where s.sno=(select sno from score where degree = (select max(degree) from score))  

4.  and c.cno = (select cno from score where degree = (select max(degree) from score))  

5.  Linq:  

6.      (  

7.          from s in Students  

8.          from c in Courses  

9.          from sc in Scores  

10.          let maxDegree = (from sss in Scores  

11.                          select sss.DEGREE  

12.                          ).Max()  

13.          let sno = (from ss in Scores  

14.                  where ss.DEGREE == maxDegree  

15.                  select ss.SNO).Single().ToString()  

16.          let cno = (from ssss in Scores  

17.                  where ssss.DEGREE == maxDegree  

18.                  select ssss.CNO).Single().ToString()  

19.          where s.SNO == sno && c.CNO == cno  

20.          select new {  

21.              s.SNO,  

22.              c.CNO  

23.          }  

24.      ).Distinct()  


12.
分组过滤

[sql] view plain copy

1.  查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。  

2.  select avg(degree) from score where cno like '3%' group by Cno having count(*)>=5  

3.  Linq:  

4.          from s in Scores  

5.          where s.CNO.StartsWith("3")  

6.          group s by s.CNO  

7.          into cc  

8.          where cc.Count() >= 5  

9.          select cc.Average( c => c.DEGREE)  

10.  Lambda:  

11.      Scores.Where( s => s.CNO.StartsWith("3") )  

12.              .GroupBy( s => s.CNO )  

13.                .Where( cc => ( cc.Count() >= 5) )  

14.                  .Select( cc => cc.Average( c => c.DEGREE) )  

15.  Linq: SqlMethod  

16.  like也可以这样写:  

17.      s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")  


13.
分组

[sql] view plain copy

1.  查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。  

2.  select avg(degree) from score where cno like '3%' group by Cno having count(*)>=5  

3.  Linq:  

4.          from s in Scores  

5.          where s.CNO.StartsWith("3")  

6.          group s by s.CNO  

7.          into cc  

8.          where cc.Count() >= 5  

9.          select cc.Average( c => c.DEGREE)  

10.  Lambda:  

11.      Scores.Where( s => s.CNO.StartsWith("3") )  

12.              .GroupBy( s => s.CNO )  

13.                .Where( cc => ( cc.Count() >= 5) )  

14.                  .Select( cc => cc.Average( c => c.DEGREE) )  

15.  Linq: SqlMethod  

16.  like也可以这样写:  

17.      s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")  


14.
多表查询

[sql] view plain copy

1.  Linq:  

2.      from c in Courses  

3.      join sc in Scores  

4.      on c.CNO equals sc.CNO  

5.      select new  

6.      {  

7.          sc.SNO,c.CNAME,sc.DEGREE  

8.      }  

9.  Lambda:  

10.      Courses.Join ( Scores, c => c.CNO,  

11.                               sc => sc.CNO,  

12.                               (c, sc) => new  

13.                                          {  

14.                                              SNO = sc.SNO,  

15.                                              CNAME = c.CNAME,  

16.                                              DEGREE = sc.DEGREE  

17.                                          })  

18.                  .Average()  



猜你喜欢

转载自blog.csdn.net/weixin_39550799/article/details/78918301
今日推荐