SQL、LINQ、Lambda 三种用法(转)

sql、linq、lambda 三种用法
颜色注释: sql linqtosql lambda qa
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 }

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐