EF中的三種linq、lambda、sql查詢方式
sql 、linq、lambda 查詢語句的區別
LINQ的書寫格式如下:from 臨時變數 in
集合物件或資料庫物件where 條件表示式
[order by條件]
select 臨時變數中被查詢的值[group by 條件]
Lambda表示式的書寫格式如下:
(引數列表) =>
表示式或者語句塊其中:引數個數:可以有多個引數,一個引數,或者無引數。引數型別:可以隱式或者顯式定義。
表示式或者語句塊:這部分就是我們平常寫函式的實現部分(函式體)。
1.查詢全部
1. 查詢Student表的所有記錄。
2. select * from student
3. Linq:
4. from s in Students
5. select s
6. Lambda:
7. Students.Select( s => s)
2 按條件查詢全部:
查詢Student表中的所有記錄的Sname、Ssex和Class列。
select sname,ssex,class from student
1. Linq:
2. from s in Students
3. select new {
4. s.SNAME,
5. s.SSEX,
6. s.CLASS
7. }
8. Lambda:
9. Students.Select( s => new {
10. SNAME = s.SNAME,SSEX = s.SSEX,CLASS = s.CLASS
11. })
3.distinct 去掉重複的
1. 查詢教師所有的單位即不重複的Depart列。
2. select distinct depart from teacher
3. Linq:
4. from t in Teachers.Distinct
5. select t.DEPART
6. Lambda:
7. Teachers.Distinct().Select( t => t.DEPART)
4.連線查詢 between and
1. 查詢Score表中成績在60到80之間的所有記錄。
2. select * from score where degree between 60 and 80
3. Linq:
4. from s in Scores
5. where s.DEGREE >= 60 && s.DEGREE < 80
6. select s
7. Lambda:
8. Scores.Where(
9. s => (
10. s.DEGREE >= 60 && s.DEGREE < 80
11. )
12. )
5.在範圍內篩選 In
1. select * from score where degree in (85,86,88)
2. Linq:
3. from s in Scores
4. where (
5. new decimal[]{85,86,88}
6. ).Contains(s.DEGREE)
7. select s
8. Lambda:
9. Scores.Where( s => new Decimal[] {85,86,88}.Contains(s.DEGREE))
6.or 條件過濾
1. 查詢Student表中"95031"班或性別為"女"的同學記錄。
2. select * from student where class ='95031' or ssex= N'女'
3. Linq:
4. from s in Students
5. where s.CLASS == "95031"
6. || s.CLASS == "女"
7. select s
8. Lambda:
9. Students.Where(s => ( s.CLASS == "95031" || s.CLASS == "女"))
7.排序
1. 以Class降序查詢Student表的所有記錄。
2. select * from student order by Class DESC
3. Linq:
4. from s in Students
5. orderby s.CLASS descending
6. select s
7. Lambda:
8. Students.OrderByDescending(s => s.CLASS)
8.count()行數查詢
1. select count(*) from student where class = '95031'
2. Linq:
3. ( from s in Students
4. where s.CLASS == "95031"
5. select s
6. ).Count()
7. Lambda:
8. Students.Where( s => s.CLASS == "95031" )
9. .Select( s => s)
10. .Count()
10.avg()平均
1. 查詢'3-105'號課程的平均分。
2. select avg(degree) from score where cno = '3-105'
3. Linq:
4. (
5. from s in Scores
6. where s.CNO == "3-105"
7. select s.DEGREE
8. ).Average()
9. Lambda:
10. Scores.Where( s => s.CNO == "3-105")
11. .Select( s => s.DEGREE)
11.子查詢
1. 查詢Score表中的最高分的學生學號和課程號。
2. select distinct s.Sno,c.Cno from student as s,course as c ,score as sc
3. where s.sno=(select sno from score where degree = (select max(degree) from score))
4. and c.cno = (select cno from score where degree = (select max(degree) from score))
5. Linq:
6. (
7. from s in Students
8. from c in Courses
9. from sc in Scores
10. let maxDegree = (from sss in Scores
11. select sss.DEGREE
12. ).Max()
13. let sno = (from ss in Scores
14. where ss.DEGREE == maxDegree
15. select ss.SNO).Single().ToString()
16. let cno = (from ssss in Scores
17. where ssss.DEGREE == maxDegree
18. select ssss.CNO).Single().ToString()
19. where s.SNO == sno && c.CNO == cno
20. select new {
21. s.SNO,
22. c.CNO
23. }
24. ).Distinct()
12.分組過濾
1. 查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。
2. select avg(degree) from score where cno like '3%' group by Cno having count(*)>=5
3. Linq:
4. from s in Scores
5. where s.CNO.StartsWith("3")
6. group s by s.CNO
7. into cc
8. where cc.Count() >= 5
9. select cc.Average( c => c.DEGREE)
10. Lambda:
11. Scores.Where( s => s.CNO.StartsWith("3") )
12. .GroupBy( s => s.CNO )
13. .Where( cc => ( cc.Count() >= 5) )
14. .Select( cc => cc.Average( c => c.DEGREE) )
15. Linq: SqlMethod
16. like也可以這樣寫:
17. s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")
13.分組
1. 查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。
2. select avg(degree) from score where cno like '3%' group by Cno having count(*)>=5
3. Linq:
4. from s in Scores
5. where s.CNO.StartsWith("3")
6. group s by s.CNO
7. into cc
8. where cc.Count() >= 5
9. select cc.Average( c => c.DEGREE)
10. Lambda:
11. Scores.Where( s => s.CNO.StartsWith("3") )
12. .GroupBy( s => s.CNO )
13. .Where( cc => ( cc.Count() >= 5) )
14. .Select( cc => cc.Average( c => c.DEGREE) )
15. Linq: SqlMethod
16. like也可以這樣寫:
17. s.CNO.StartsWith("3") or SqlMethods.Like(s.CNO,"%3")
14. 多表查詢
1. Linq:
2. from c in Courses
3. join sc in Scores
4. on c.CNO equals sc.CNO
5. select new
6. {
7. sc.SNO,c.CNAME,sc.DEGREE
8. }
9. Lambda:
10. Courses.Join ( Scores, c => c.CNO,
11. sc => sc.CNO,
12. (c, sc) => new
13. {
14. SNO = sc.SNO,
15. CNAME = c.CNAME,
16. DEGREE = sc.DEGREE
17. })
18. .Average()