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

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

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

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

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

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

1.查询全部

查询Student表的所有记录。
  select * from student
  Linq:
  from s in Students
  select s
  Lambda:
  Students.Select( s => s)
 
2 按条件查询全部:

查询Student表中的所有记录的Sname、Ssex和Class列。
 select sname,ssex,class from student
  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
  })

3.distinct 去掉重复的

查询教师所有的单位即不重复的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

查询Score表中成绩在60到80之间的所有记录。
 select * from score where degree between 60 and 80
 Linq:
 from s in Scores
 where s.DEGREE >= 60 && s.DEGREE < 80
 select s
 Lambda:
 Scores.Where(
 s => (
 s.DEGREE >= 60 && s.DEGREE < 80
 )
 )

5.在范围内筛选 In

select * from score where degree in (85,86,88)
 Linq:
 from s in Scores
 where (
 new decimal[]{85,86,88}
 ).Contains(s.DEGREE)
 select s
 Lambda:
 Scores.Where( s => new Decimal[] {85,86,88}.Contains(s.DEGREE))

6.or 条件过滤

查询Student表中"95031"班或性别为"女"的同学记录。
 select * from student where class ='95031' or ssex= N'女'
 Linq:
 from s in Students
 where s.CLASS == "95031"
 || s.CLASS == "女"
 select s
 Lambda:
 Students.Where(s => ( s.CLASS == "95031" || s.CLASS == "女"))

7.排序

以Class降序查询Student表的所有记录。
 select * from student order by Class DESC
 Linq:
 from s in Students
 orderby s.CLASS descending
 select s
 Lambda:
 Students.OrderByDescending(s => s.CLASS)
 count()行数查询

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.avg()平均

查询'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)

11.子查询

查询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()

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

13.分组

查询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. 多表查询

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()

sql 、linq、lambda 总结的更多相关文章

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

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

  2. SQL,Linq,Lambda之间的转换练习

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

  3. sql linq lambda 对比

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

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

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

  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. ASP.NET EF(LINQ/Lambda查询)

    EF(EntityFrameWork) ORM(对象关系映射框架/数据持久化框架),根据实体对象操作数据表中数据的一种面向对象的操作框架,底层也是调用ADO.NET ASP.NET MVC 项目会自动 ...

  8. [算法1-排序](.NET源码学习)& LINQ & Lambda

    [算法1-排序](.NET源码学习)& LINQ & Lambda 说起排序算法,在日常实际开发中我们基本不在意这些事情,有API不用不是没事找事嘛.但必要的基础还是需要了解掌握. 排 ...

  9. sql,lambda,linq语句

    实例 Code 查询Student表的所有记录. select * from student Linq: from s in Students select s Lambda: Students.Se ...

  10. Linq lambda 匿名方法

    课程6 委托.匿名方法.Lambda表达式.Linq查询表达式 上课日志1 一.委托的基本认识 提问:能不能把方法作为参数传递??? 也即是能不能声明一个能存放方法的变量呢——委托. 委托是一种数据类 ...

随机推荐

  1. centOS目录结构

    rpm包到  /usr/bin/dirName 二进制包(需要解压 -> 生成配置文件 -> make(编译) -> 安装)放到 /usr/local/src/** mongodb ...

  2. MVC4过滤器

    MVC4提供的四种基本类型过滤器接口,IAuthorizationFilter.IActionFilter.IResultFilter和IExceptionFilter,可通过继承对应的接口和Filt ...

  3. 第二课 less的学习以及移动端需要注意的问题

    一.LESS的学习笔记: 1.less介绍:一种动态样式语言.less将css赋予了动态语言的特性,如变量,继承,运算,函数,less既可以在客户端上运行(支持IE6+,webkit,firefox) ...

  4. JAVA作业02

    一,      课堂练习 (一)构造方法 1,源代码 public class Test{ public static void main(String[] args){ Foo obj1=new F ...

  5. js 闭包原理理解

    问题?什么是js(JavaScript)的闭包原理,有什么作用? 一.定义 官方解释:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 很显然 ...

  6. Java中length,length(),size()区别

    length属性:用于获取数组长度. eg: int ar[] = new int{1,2,3} /** * 数组用length属性取得长度 */ int lenAr = ar.length;//此处 ...

  7. [Win32命令行] 更改提示符字符串(PS1)

    当进入的目录比较深时, cmd的提示符几乎会占据整行, 很烦, 于是Google之... 参考: A better PROMPT for CMD.EXE ... 更改方式:        1. pro ...

  8. CentOS版本选择说明

    官方下载站http://www.centos.org/download/ 所有版本下载地址http://vault.centos.org/ 首先对一些镜像文件做个简单的介绍: LiveCD一般用来修复 ...

  9. HDU4288 Coder(线段树)

    注意添加到集合中的数是升序的,先将数据读入,再离散化. sum[rt][i]表示此节点的区域位置对5取模为i的数的和,删除一个数则右边的数循环左移一位,添加一个数则右边数循环右移一位,相当于循环左移4 ...

  10. 攻城狮在路上(叁)Linux(二十八)--- 打包命令:tar

    首先介绍一下tar打包命令的基本格式,下面的三种之间不能混淆. tar [-j|-z] [cv] [-f 新文件名] file1 file2 ...; tar [-j|-z] [tv] [-f 新文件 ...