通过16道练习学习Linq和Lambda.docx

上传人:b****7 文档编号:10582489 上传时间:2023-02-21 格式:DOCX 页数:8 大小:16.46KB
下载 相关 举报
通过16道练习学习Linq和Lambda.docx_第1页
第1页 / 共8页
通过16道练习学习Linq和Lambda.docx_第2页
第2页 / 共8页
通过16道练习学习Linq和Lambda.docx_第3页
第3页 / 共8页
通过16道练习学习Linq和Lambda.docx_第4页
第4页 / 共8页
通过16道练习学习Linq和Lambda.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

通过16道练习学习Linq和Lambda.docx

《通过16道练习学习Linq和Lambda.docx》由会员分享,可在线阅读,更多相关《通过16道练习学习Linq和Lambda.docx(8页珍藏版)》请在冰豆网上搜索。

通过16道练习学习Linq和Lambda.docx

通过16道练习学习Linq和Lambda

通过16道练习学习Linq和Lambda

1、查询Student表中的所有记录的Sname、Ssex和Class列。

 

selectsname,ssex,classfromstudent

Linq:

fromsinStudents

selectnew{

s.SNAME,

s.SSEX,

s.CLASS

}

Lambda:

Students.Select(s=>new{

SNAME=s.SNAME,SSEX=s.SSEX,CLASS=s.CLASS

})

2、查询教师所有的单位即不重复的Depart列。

 

selectdistinctdepartfromteacher

Linq:

fromtinTeachers.Distinct()

selectt.DEPART

Lambda:

Teachers.Distinct().Select(t=>t.DEPART)

 

3、查询Student表的所有记录。

 

select*fromstudent

Linq:

fromsinStudents

selects

Lambda:

Students.Select(s=>s)

 

4、查询Score表中成绩在60到80之间的所有记录。

 

select*fromscorewheredegreebetween60and80

Linq:

fromsinScores

wheres.DEGREE>=60&&s.DEGREE<80

selects

Lambda:

Scores.Where(

s=>(

s.DEGREE>=60&&s.DEGREE<80

 

5、查询Score表中成绩为85,86或88的记录。

 

select*fromscorewheredegreein(85,86,88)

Linq:

In

fromsinScores

where(

newdecimal[]{85,86,88}

).Contains(s.DEGREE)

selects

Lambda:

Scores.Where(s=>newDecimal[]{85,86,88}.Contains(s.DEGREE))

Notin

fromsinScores

where!

newdecimal[]{85,86,88}

).Contains(s.DEGREE)

selects

Lambda:

Scores.Where(s=>!

(newDecimal[]{85,86,88}.Contains(s.DEGREE)))

  Any()应用:

双表进行Any时,必须是主键为(String)

  CustomerDemographicsCustomerTypeID(String)

  CustomerCustomerDemos(CustomerIDCustomerTypeID)(String)

  一个主键与二个主建进行Any(或者是一对一关键进行Any)不可,以二个主键于与一个主键进行Any

fromeinCustomerDemographics

where!

e.CustomerCustomerDemos.Any()

selecte

fromcinCategories

where!

c.Products.Any()

selectc

 

 

6、查询Student表中"95031"班或性别为"女"的同学记录。

 

select*fromstudentwhereclass='95031'orssex=N'女'

Linq:

fromsinStudents

wheres.CLASS=="95031"

||s.CLASS=="女"

selects

Lambda:

Students.Where(s=>(s.CLASS=="95031"||s.CLASS=="女"))

 

7、以Class降序查询Student表的所有记录。

select*fromstudentorderbyClassDESC

Linq:

fromsinStudents

orderbys.CLASSdescending

selects

Lambda:

Students.OrderByDescending(s=>s.CLASS)

 

8、以Cno升序、Degree降序查询Score表的所有记录。

 

select*fromscoreorderbyCnoASC,DegreeDESC

Linq:

(这里CnoASC在linq中要写在最外面)

fromsinScores

orderbys.DEGREEdescending

orderbys.CNOascending

selects

Lambda:

Scores.OrderByDescending(s=>s.DEGREE)

.OrderBy(s=>s.CNO)

 

9、查询"95031"班的学生人数。

selectcount(*)fromstudentwhereclass='95031'

Linq:

(fromsinStudents

wheres.CLASS=="95031"

selects

).Count()

Lambda:

Students.Where(s=>s.CLASS=="95031")

.Select(s=>s)

.Count()

 

10、查询Score表中的最高分的学生学号和课程号。

 

selectdistincts.Sno,c.Cnofromstudentass,courseasc,scoreassc

wheres.sno=(selectsnofromscorewheredegree=(selectmax(degree)fromscore))

ando=(selectcnofromscorewheredegree=(selectmax(degree)fromscore))

Linq:

fromsinStudents

fromcinCourses

fromscinScores

letmaxDegree=(fromsssinScores

selectsss.DEGREE

).Max()

letsno=(fromssinScores

wheress.DEGREE==maxDegree

selectss.SNO).Single().ToString()

letcno=(fromssssinScores

wheressss.DEGREE==maxDegree

selectssss.CNO).Single().ToString()

wheres.SNO==sno&&c.CNO==cno

selectnew{

s.SNO,

c.CNO

}

).Distinct()

  操作时问题?

执行时报错:

wheres.SNO==sno(这行报出来的)运算符"=="无法应用于"string"和"System.Linq.IQueryable"类型的操作数

  解决:

  原:

letsno=(fromssinScores

               wheress.DEGREE==maxDegree

               selectss.SNO).ToString()

    Queryable().Single()返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。

  解:

letsno=(fromssinScores

               wheress.DEGREE==maxDegree

               selectss.SNO).Single().ToString()

 

11、查询'3-105'号课程的平均分。

selectavg(degree)fromscorewherecno='3-105'

Linq:

fromsinScores

wheres.CNO=="3-105"

selects.DEGREE

).Average()

Lambda:

Scores.Where(s=>s.CNO=="3-105")

.Select(s=>s.DEGREE)

.Average()

 

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

selectavg(degree)fromscorewherecnolike'3%'groupbyCnohavingcount(*)>=5

Linq:

fromsinScores

wheres.CNO.StartsWith("3")

groupsbys.CNO

intocc

wherecc.Count()>=5

selectcc.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")orSqlMethods.Like(s.CNO,"%3")

13、查询最低分大于70,最高分小于90的Sno列。

selectsnofromscoregroupbysnohavingmin(degree)>70andmax(degree)<90

Linq:

fromsinScores

groupsbys.SNO

intoss

wheress.Min(cc=>cc.DEGREE)>70&&ss.Max(cc=>cc.DEGREE)<90

selectnew

{

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列。

selects.sname,o,sc.degreefromstudentass,scoreasscwheres.sno=sc.sno

Linq:

fromsinStudents

joinscinScores

ons.SNOequalssc.SNO

selectnew

{

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列。

selectsc.sno,ame,sc.degreefromcourseasc,scoreasscwhereo=o

Linq:

fromcinCourses

joinscinScores

onc.CNOequalssc.CNO

selectnew

{

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列。

selects.sname,ame,sc.degreefromstudentass,courseasc,scoreasscwheres.sno=sc.snoando=o

Linq:

fromsinStudents

fromcinCourses

fromscinScores

wheres.SNO==sc.SNO&&c.CNO==sc.CNO

selectnew{s.SNAME,c.CNAME,sc.DEGREE}

 

主要参考文章链接:

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 临床医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1