• 浅谈sql 、linq、lambda 查询语句的区别


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

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

    Lambda表达式的书写格式如下:

    (参数列表) => 表达式或者语句块

    其中: 参数个数:可以有多个参数,一个参数,或者无参数。

    参数类型:可以隐式或者显式定义。

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

    1.查询全部

    实例 Code
    查询Student表的所有记录。
    1 select * from student
    2 Linq:
    3     from s in Students
    4     select s
    5 Lambda:
    6     Students.Select( s => s) 

    2 按条件查询全部:

    实例 Code
     查询Student表中的所有记录的Sname、Ssex和Class列。
     1  select sname,ssex,class from student
     2  Linq:
     3      from s in Students
     4      select new {
     5          s.SNAME,
     6          s.SSEX,
     7          s.CLASS
     8      }
     9 Lambda:
    10     Students.Select( s =>new {
    11         SNAME = s.SNAME,SSEX = s.SSEX,CLASS = s.CLASS
    12     })

    3.distinct 去掉重复的

    实例 Code
    查询教师所有的单位即不重复的Depart列。
    select distinct depart from teacher
    Linq:
        from t in Teachers.Distinct()
        select t.DEPART
    Lambda:
        Teachers.Distinct().Select( t => t.DEPART) 
    

      

    4.连接查询 between and

    实例 Code
    查询Score表中成绩在60到80之间的所有记录。
     1 select * from score where degree between 60 and 80
     2  Linq:
     3      from s in Scores
     4  where s.DEGREE >=60&& s.DEGREE <80
     5      select s
     6  Lambda:
     7      Scores.Where(
     8          s => (
     9                 s.DEGREE >=60&& s.DEGREE <80
    10              )
    11     ) 

    5.在范围内筛选 In

    实例 Code
     
    1 select * from score where degree in (85,86,88)
    2 Linq:
    3     from s in Scores
    4 where (
    5 newdecimal[]{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 条件过滤

    实例 Code
     
    查询Student表中"95031"班或性别为""的同学记录。
    1 select * from student whereclass='95031' or ssex= N''
    2 Linq:
    3     from s in Students
    4 where s.CLASS =="95031"
    5 || s.CLASS =="女"
    6     select s
    7 Lambda:
    8     Students.Where(s => ( s.CLASS =="95031"|| s.CLASS =="女")) 
     

    7.排序

    实例 Code
    以Class降序查询Student表的所有记录。
    1 select * from student order by Class DESC
    2 Linq:
    3     from s in Students
    4     orderby s.CLASS descending
    5     select s
    6 Lambda:
    7     Students.OrderByDescending(s => s.CLASS)

    8.count()行数查询

    实例 Code
     1 select count(*) from student whereclass='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()平均

    实例 Code
    查询'3-105'号课程的平均分。
     1 select avg(degree) from score where cno ='3-105'
     2  Linq:
     3      (
     4          from s in Scores
     5  where s.CNO =="3-105"
     6          select s.DEGREE
     7      ).Average()
     8  Lambda:
     9     Scores.Where( s => s.CNO =="3-105")
    10             .Select( s => s.DEGREE)

    11.子查询

    实例 Code
    查询Score表中的最高分的学生学号和课程号。
     1  select distinct s.Sno,c.Cno from student as s,course as c ,score as sc
     2  where s.sno=(select sno from score where degree = (select max(degree) from score))
     3  and c.cno = (select cno from score where degree = (select max(degree) from score))
     4  Linq:
     5      (
     6          from s in Students
     7          from c in Courses
     8          from sc in Scores
     9         let maxDegree = (from sss in Scores
    10                         select sss.DEGREE
    11                         ).Max()
    12         let sno = (from ss in Scores
    13 where ss.DEGREE == maxDegree
    14                 select ss.SNO).Single().ToString()
    15         let cno = (from ssss in Scores
    16 where ssss.DEGREE == maxDegree
    17                 select ssss.CNO).Single().ToString()
    18 where s.SNO == sno && c.CNO == cno
    19         select new {
    20             s.SNO,
    21             c.CNO
    22         }
    23     ).Distinct()

    12.分组 过滤

    实例 Code
    查询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) )
    Linq: SqlMethod
    like也可以这样写:
        s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3") 

    13.分组

    实例 Code
    查询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) )
    Linq: SqlMethod
    like也可以这样写:
        s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3") 

    14. 多表查询

    实例 Code

     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.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
                                            })
                    .Average() 
  • 相关阅读:
    连接mysql慢或者多台服务器ping不通mysql
    java8 Stream
    Idea 提示xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
    sublime Text的使用
    BigDecimal 加减乘除 比较大小 setScale(精度处理)
    kubectl 命令
    mac brew命令的使用
    mysql json
    idea open打开项目之后,project里没有目录结构。
    mac 多显示器焦点快速切换
  • 原文地址:https://www.cnblogs.com/caosenianhuan/p/3158613.html
Copyright © 2020-2023  润新知