linq与lambda 常用查询语句写法对比
LINQ的书写格式如下:
from 临时变量 in 集合对象或数据库对象
where 条件表达式
[order by条件]
select 临时变量中被查询的值
[group by 条件]
Lambda表达式的书写格式如下:
(参数列表) => 表达式或者语句块
其中: 参数个数:可以有多个参数,一个参数,或者无参数。
参数类型:可以隐式或者显式定义。
表达式或者语句块:这部分就是我们平常写函数的实现部分(函数体)。
1.查询全部

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

2 按条件查询全部:

实例 Code
查询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 去掉重复的

实例 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之间的所有记录。
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

实例 Code
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 条件过滤

实例 Code
查询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.排序

实例 Code
以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)

8.count()行数查询

实例 Code
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()

9.avg()平均

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

10.子查询

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

11.分组 过滤

实例 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")

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
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()
--自己的例子
IList<DCKeyword> mDCKeywordJoinLst = mDCKeywordLst.Join(
mDCKeywordImportants,
word => word.keyword_id,
impWord => impWord.keyword_id,
(word, impWord) => word
).ToList(); //重点关键词

linq与lambda 常用查询语句写法对比的更多相关文章
- Linq与Lambda常用查询语法
1.查询全部 2.按条件查询全部 3.去除重复 4.连接查询 between and 5.排序 6.分组
- 23个MySQL常用查询语句
23个MySQL常用查询语句 一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!> ...
- Hibernate常用查询语句
Hibernate常用查询语句 Hib的检索方式1'导航对象图检索方式.通过已经加载的对象,调用.iterator()方法可以得到order对象如果是首次执行此方法,Hib会从数据库加载关联的orde ...
- LINQ中的一些查询语句格式
LINQ的基本格式如下所示:var <变量> = from <项目> in <数据源> where <表达式> orderby <表达式> ...
- MySQL常用查询语句汇总(不定时更新)
在这篇文章中我会通过一些例子来介绍日常编程中常用的SQL语句 目录: ## 1.数据库的建立 ## 1.数据库的建立 实例将ER图的形式给出: 由此转换的4个关系模式: ...
- mysql—常用查询语句总结
关于MySQL常用的查询语句 一查询数值型数据: ; 查询谓词:>,=,<,<>,!=,!>,!<,=>,=< 二查询字符串 SELECT * FROM ...
- PHP-- 三种数据库随机查询语句写法
1. Oracle,随机查询查询语句-20条 select * from ( select * from 表名 order by dbms_random.value ) where rownum ...
- Cisco 路由交换 常用查询语句
基本信息查询语句 #查看全配置信息 #show running-configure #查看vlan信息 #show vlan brief #查看物理直连信息 #show cdp neighbors d ...
- pg_sql常用查询语句整理
#pg_sql之增删改查 #修改: inset into table_name (id, name, age, address ) select replace(old_id,old_id,new_i ...
随机推荐
- mysql 5.5.42 更改数据目录 centos 6.5环境
1.新建新数据目录,检查目录属主机权限,一般情况下属于mysql组,myql用户,因为我们安装mysql的时候会新建该账户和组. 2.目录权限检查完毕,停止数据库服务. 3.移动数据目录 ,我用的是m ...
- jenkins slave 挂载
http://blog.sina.com.cn/s/blog_13cc013b50102wiau.html
- gulp合并压缩
1.文件合并压缩 var concat = require(‘gulp-concat’); //引用 var uglify = require(‘gulp-uglify’); 接下来,只要conca ...
- cmd导出oracle数据库数据
今天弄了下oracle数据库导入导出命令exp,imp 首先这个命令是在cmd直接执行,不是sqlplus登录后再执行,见下图: 再次,注意结尾不能有分号(;): exp scott/scott@su ...
- js call().apply().bind()的用法
function Person(age) { this.age = age; } Person.prototype.sayHi = function (x, y) { console.log((x + ...
- IOCP详解
http://blog.csdn.net/piggyxp/article/details/6922277 ps: 原作者很厉害了, 把一个iocp模型讲解的这么形象,不过在实践过程中发现一些细节说得有 ...
- jekyll建站详细教程
Jekyll是一款静态博客生成器,也是github page支持的后台引擎,所以如果你有以下需求,极力推荐使用jekyll搭建博客,>>浏览我的博客 个性化的展示界面,站点逻辑 个性化的域 ...
- 【转】 不需要任何权限获得Android设备的唯一ID
不需要任何权限获得Android设备的唯一ID,权限android设备id 这个问题来自于Is there a unique Android device ID? 我对这个问题的答案做了整理,包括将另 ...
- innodb_flush_log_at_trx_commit
innodb_flush_log_at_trx_commit innodb_buffer_pool_size如 果用Innodb,那么这是一个重要变量.相对于MyISAM来说,Innodb对于bu ...
- JavaWeb总结(六)
获取原始表单数据 - POST请求不仅可以传输文本信息还可以传输二进制数据 - 如果想得到请求中参数的原始字节数据,可以使用HttpServletRequest对象提供的getInputSteam() ...