没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
linq 语法
sql 语句-linq 语言-lambda 表达式对照
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()
剩余24页未读,继续阅读
资源评论
- huaix黑夜2013-01-15新手学习 lambda,感谢分享
- 3q2008Com2018-07-19学习学习学习
hxinshenc
- 粉丝: 0
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Edge浏览器下载文件提示 “无法安全下载” 的解决方法
- 基于springboot+layui的医院日常耗材管理系统.zip
- 计算机毕业设计-ASP.NET教育报表管理系统-权限管理模块(源代码+)-毕设源码实例.zip
- 计算机毕业设计-ASP.NET教务信息管理系统的设计与实现(源代码+)-毕设源码实例.zip
- 免费计算机毕业设计-线上公司求职招聘系统的设计与实现(包含论文+源码)
- Eleven的精益供应链管理-碓胤咨询龚胤全.rar
- 5套光伏、储能、充电收益测算表.zip
- C2 供应链集成演示平台操作手册(详细版).rar
- 3套光储充一体化站CAD+PDF图纸.zip
- c++游戏开发,本人开发的c++小游戏飞机大战(二)源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功