linq lanbda表达式的用法
1、 查询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
})
2、
查询教师所有的单位即不重复的Depart列。
select
distinct depart from teacher
Linq:
from t
in Teachers.Distinct()
select t.DEPART
Lambda:
Teachers.Distinct().Select( t =>
t.DEPART)
3、
查询Student表的所有记录。
select *
from student
Linq:
from s in Students
select s
Lambda:
Students.Select( s => s)
4、
查询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、
查询Score表中成绩为85,86或88的记录。
select *
from score where degree in (85,86,88)
Linq:
In
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))
Not
in
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)))
Any()应用:双表进行Any时,必须是主键为(String)
CustomerDemographics
CustomerTypeID(String)
CustomerCustomerDemos (CustomerID
CustomerTypeID) (String)
一个主键与二个主建进行Any(或者是一对一关键进行Any)
不可,以二个主键于与一个主键进行Any
from e in
CustomerDemographics
where !e.CustomerCustomerDemos.Any()
select
e
from c in Categories
where !c.Products.Any()
select c
6、
查询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)
8、
以Cno升序、Degree降序查询Score表的所有记录。
select *
from score order by Cno ASC,Degree DESC
Linq:(这里Cno ASC在linq中要写在最外面)
from s in Scores
orderby
s.DEGREE descending
orderby s.CNO ascending
select
s
Lambda:
Scores.OrderByDescending( s => s.DEGREE)
.OrderBy( s =>
s.CNO)
9、
查询"95031"班的学生人数。
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、查询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()
操作时问题?执行时报错: where s.SNO == sno(这行报出来的) 运算符"=="无法应用于"string"和"System.Linq.IQueryable<string>"类型的操作数
解决:
原:let
sno = (from ss in Scores
where ss.DEGREE ==
maxDegree
select
ss.SNO).ToString()
Queryable().Single()返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。
解:let
sno = (from ss in Scores
where ss.DEGREE ==
maxDegree
select
ss.SNO).Single().ToString()
11、查询'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)
.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) )
Linq: SqlMethod
like也可以这样写:
s.CNO.StartsWith("3")
or SqlMethods.Like(s.CNO,"%3")
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.SNAME,
CNO =
sc.CNO,
DEGREE =
sc.DEGREE
})
15、查询所有学生的Sno、Cname和Degree列。
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
})
16、查询所有学生的Sname、Cname和Degree列。
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 }
linq lanbda表达式的用法的更多相关文章
- C# LINQ查询表达式用法对应Lambda表达式
C#编程语言非常优美,我个人还是非常赞同的.特别是在学习一段时间C#后发现确实在它的语法和美观度来说确实要比其它编程语言强一些(也可能是由于VS编译器的加持)用起来非常舒服,而且对于C#我觉得他最优美 ...
- LinQ To Object 基本用法
http://www.cnblogs.com/terryzh/archive/2012/11/10/2763538.html LinQ To Object 基本用法 inq的基本语法:var resu ...
- LINQ查询表达式和LAMBDA点标记方法基础
在上一篇文章中,我们介绍了LINQ的一些基本用法,这一篇我们来看下另一种更简洁更优雅的表达式,Lambda表达式,也可以叫做点标记方法. 相信大家在实际工作中都用到过这两种方式,下面我们还是用实例来看 ...
- Linq专题之创建Linq查询表达式
本节我们主要介绍一下如何创建查询集合类型,关系数据库类型,DataSet对象类型和XML类型的数据源的Linq查询表达式. 下面在实例代码ReadyCollectionData()函数创建了准备的数据 ...
- Linq lamda表达式Single和First方法
让我们来看看如何对一个整数数组使用 Single 操作符.这个整数数组的每个元素代表 2 的 1 到 10 次方.先创建此数组,然后使用 Single 操作符来检索满足 Linq Lambda表达 ...
- 2.3 LINQ查询表达式中 使用select子句 指定目标数据
本篇讲解LINQ查询的三种形式: 查询对象 自定义查询对象某个属性 查询匿名类型结果 [1.查询结果返回集合元素] 在LINQ查询中,select子句和from子句都是必备子句.LINQ查询表达式必须 ...
- SQL进阶1:case表达式的用法示例
一:case表达式的用法 1.SQL中的case表达式的作用是用来对"某个变量"进行某种转化,通常在select字句中使用,举个例子: 不能看出,case表达式很像我们的if el ...
- LINQ查询表达式---------let子句
LINQ查询表达式---------let子句 let子句创建一个范围变量来存储结果,变量被创建后,不能修改或把其他表达式的结果重新赋值给它.此范围变量可以再后续的LINQ子句中使用. class P ...
- LINQ查询表达式---------join子句
LINQ查询表达式---------join子句 join 子句接受两个源序列作为输入. 每个序列中的元素都必须是可以与另一个序列中的相应属性进行比较的属性,或者包含一个这样的属性. join子句使用 ...
随机推荐
- AngularJs基础总结(1.4版本)
注明:现在用的是最新的1系列1.4版本. 一.细节方面入手: 1,ng-app根节点,一般别写在html上面,最多也就写在body就行了,因为我们难免会忘记这个写在哪里,然后复制一些案例代码却总报错. ...
- java 练手 谁是最好的Coder
Problem A 谁是最好的Coder 时间限制:1000 ms | 内存限制:65535 KB 描述 计科班有很多Coder,帅帅想知道自己是不是综合实力最强的coder. 帅帅喜欢帅,所 ...
- IDEA之web项目(maven项目)创建
1.下载IDEA付费版,有30天的试用期,免费版创建不了web项目(导入不了tomcat). 网址:IntelliJ IDEA :: Download Latest Version of Intell ...
- DOM之节点层次
1.1 Node类型 DOM1级定义了一个Node接口,该接口将由DOM中的所有节点类型实现.这个Node接口在JS中是作为Node类型实现的:除了IE之外,其他浏览器可访问这个类型.JS中的所有节点 ...
- HDU 1064 Financial Management
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1064 解题报告:用来凑个题数吧,看题的时间比过题的时间多的多,就是输入12个浮点数,然后输出平均数,只 ...
- 使用update!导致的更新时候的错误信息不显示 ruby on rails
在图片管理里添加了校验方法之后,发现在更新的时候页面不显示校验报错的信息 class Picture < ApplicationRecord belongs_to :imageable, pol ...
- 内网安全工具之hscan扫描
工具下载地址:hscan1.2.zip 界面简单,看配置: 这里我们主要需要配置的是模块和参数 模块,按照默认配置就行,取消 check HTTP vulnerability(漏洞检测) 会更快一点. ...
- hiho #1326 : 有序01字符串
#1326 : 有序01字符串 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于一个01字符串,你每次可以将一个0修改成1,或者将一个1修改成0.那么,你最少需要修改 ...
- qt-5.6.0 移植之实现板子与ubuntu主机通过网络进行文件传输
经过一上午的调试以及同事的帮助,终于实现板子与主机的文件传输. 第一步关闭所有的防火墙 在 Windows 里面是在控制面板->安全->Windows 防火墙->自定义设置 在ubu ...
- net-snmp配置:snmp v3的安全配置
net-snmp配置:snmp v3的安全配置 net-snmp配置:snmp v3的安全配置 增加snmp v3用户 增加 认证且加密只读账号(authPriv) 增加 认证且加密的读写账户 增加 ...