一起谈.NET技术,通过16道练习学习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()
      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 }

 

主要参考文章链接:http://www.cnblogs.com/RuiLei/archive/2008/11/09/1329905.html

时间: 2024-09-07 16:55:10

一起谈.NET技术,通过16道练习学习Linq和Lambda的相关文章

通过16道练习学习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列

一起谈.NET技术,C#中的委托,匿名方法和Lambda表达式

简介 在.NET中,委托,匿名方法和Lambda表达式很容易发生混淆.我想下面的代码能证实这点.下面哪一个First会被编译?哪一个会返回我们需要的结果?即Customer.ID=5.答案是6个First不仅被编译,并都获得正确答案,且他们的结果一样.如果你对此感到困惑,那么请继续看这篇文章. class Customer { public int ID { get; set; } public static bool Test(Customer x) { return x.ID == 5; }

(转)精益技术简历之道——改善技术简历的47条原则

作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文链接:http://lucida.me/blog/lean-technical-resume/ 关于 这篇文章围绕着技术简历这个话题,从版式.个人信息.技术能力.项目经历和教育背景等方面出发,给出了编写高质量技术简历所应遵循的47条原则.它们既有益于求职者编写高质量的简历,也有助于招聘者筛选合格的求职者. 为什么要编写这篇文章? 技术简历既是技术人员求职必不可少的一环,也是找工作的第一步,其重要性不言而喻.然而关于如何编

《创业家》牛文文:少谈点模式多谈点技术

"模式"如同当年的"主义",流行于各种创业大赛.创业励志节目.论坛的"街头"式秀场 文/创业家 牛文文 "美国某某公司你知道吧?就是刚被戴尔.惠普.思科十几亿美元抢购的那家.我们的模式和它的一样,现在还没赢利,可将来起码有十几亿人民币的市值." "我开了小煤矿,但煤运不出去,上商学院之后受到启发,想搞模式创新,具体讲就是想在铁路边上搞个煤炭物流开发区,建一个大的物流和信息流平台,把分散的煤炭集中在我这个园区,这样和铁

一起谈.NET技术,学习Linq经验总结

Linq有很多值得学习的地方,这里我们主要介绍学习Linq,包括介绍Linq目标是实现语言与数据的深度结合等方面. 上一个系列讲了C#3.0的新特性,为学习Linq做好了铺垫:接下来的一段时间转入学习Linq,上述新特性也会在介绍的过程中提及到. 学习Linq 在我们的软件中,数据的重要性不可言喻,特别是象ERP,CRM等等这类商业应用软件就是围绕着数据转:然而数据的来源各种各样,如存放在内存中的业务对象.存放在xml文件的数据.SqlServer关系数据库...这些数据源的读取操作各不相同,相

一起谈.NET技术,WebForm:毒药还是利器?

一.Webform的诞生及运行机制,web开发带来的革命性变化 九十年代中期,Internet崭露头角.为了进军Web应用程序行业,微软开发了Active ServerPages(ASP).ASP是开发Web页面的一种快速.简便的方式.ASP页面由一个页面组成,其中包含了标记和语言的混合.ASP的强大之处在于,在页面发送给终端用户的Web浏览器之前,可以在页面上包含在Web服务器上执行的VBScript或JScript代码指令.这是创建动态Web页面的一种简单方式,动态Web页面可以根据开发人员

云计算技术:跟我一起学习 SmartCloud Orchestrator

由于篇幅限制,本文所介绍的功能不会覆盖 SmartCloud Orchestrator 的所有方面.但是通过本文,用户可以清楚的了解 IBM SmartCloud Orchestrator 的体系结构.产品安装.服务器配置和产品特性. 产品架构 如图 1 所示,IBM SmartCloud Orchestrator 捆绑了多个产品和组件,这些产品和组件组合在一起用以构建复杂的业务架构. 图 1.IBM SmartCloud    Orchestrator 架构图 云计算技术:跟我一起学习 Sma

【阿里云资讯】最前沿人工智能,助力双11搜索推荐技术再升级——深度增强学习大规模在线应用

11月12日消息,天猫"双11"销售额6分58秒破百亿:前30分钟内交易峰值17.5万笔/秒,支付峰值12万笔/秒,24小时实现销售额1207亿元.用户更快.更准购物体验来自于搜索和推荐的数据智能的提升.   去年双11期间,搜索事业部因为采用个性化推荐技术给业务带来显著提升而获得阿里巴巴最高奖"CEO奖",今年技术再度升级,规模化上线最前沿的人工智能技术深度增强学习与自适应在线学习,用户点击率提升10-20%. 阿里搜索和推荐技术负责人王志荣表示,双十一的搜索与推

从 Visual Studio 2017 谈起,解析微软技术生态进化之道

曾经被业界取笑「闭关锁国」的微软如今也走向了「改革开放」的道路,Visual Studio 2017的发布,不仅是VS二十周年的大事件,更是微软技术生态焕然一新的直观体验.以前只支持Windows及自家产品的微软,现在iOS.Android.Mac都支持了.写在前面 北京时间2017年3月8日凌晨,Visual Studio 2017如期发布.今年恰逢Visual Studio二十周年,Visual Studio团队可谓诚意满满.不负众望--VS2017不仅拥有全新的模块化设计和更强的性能,功能