linq與lambda 常用查詢語句寫法對比
原文地址:http://www.cnblogs.com/xust/articles/2700244.html
LINQ的書寫格式如下:
from 臨時變量 in 集合對象或數據庫對象
where 條件表達式
[order by條件]
select 臨時變量中被查詢的值
[group by 條件]
Lambda表達式的書寫格式如下:
(參數列表) => 表達式或者語句塊
其中: 參數個數:可以有多個參數,一個參數,或者無參數。
參數類型:可以隱式或者顯式定義。
表達式或者語句塊:這部分就是我們平常寫函數的實現部分(函數體)。
1.查詢全部
實例 Code
查詢Student表的所有記錄。
select * from student
Linq:
from s in Students
select s
Lambda:
Students.Select( s => s)
2 按條件查詢全部:
實例 Code
查詢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
})
3.distinct 去掉重復的
實例 Code
查詢教師所有的單位即不重復的Depart列。
select distinct depart from teacher
Linq:
from t in Teachers.Distinct()
select t.DEPART
Lambda:
Teachers.Distinct().Select( t => t.DEPART)
4.連接查詢 between and
實例 Code
查詢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.在范圍內篩選 In
實例 Code
select * from score where degree in (85,86,88)
Linq:
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))
6.or 條件過濾
實例 Code
查詢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.排序
實例 Code
以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.count()行數查詢
實例 Code
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()
9.avg()平均
實例 Code
查詢'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)
10.子查詢
實例 Code
查詢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()
11.分組 過濾
實例 Code
查詢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")
12.分組
實例 Code
查詢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. 多表查詢
實例 Code
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
})
.Average()
--自己的例子
IList<DCKeyword> mDCKeywordJoinLst = mDCKeywordLst.Join(
mDCKeywordImportants,
word => word.keyword_id,
impWord => impWord.keyword_id,
(word, impWord) => word
).ToList(); //重點關鍵詞

