1. 程式人生 > >[.net core自我修煉2]SQL、LINQ和Lambda表示式

[.net core自我修煉2]SQL、LINQ和Lambda表示式

隨便說說

自己想到什麼就記錄下來了,都是自己遇到的技術點,湊合看吧。在寫後端的時候,最常用的是Lambda表示式,這就記錄一下SQL、LINQ和Lambda的不同之處。
參考資料:https://blog.csdn.net/u010926964/article/details/46240215

簡單介紹

LINQ(Language Integrate Query)是語言整合查詢他在物件和資料之間建立一種對應的關係,可以使用訪問記憶體物件的方式查詢資料集合。LINQ查詢是C#中的一種語言構造。因此開發人員可以再C#程式碼彙總巢狀類似於SQL語句的查詢表示式,從而實現資料查詢的功能。LINQ也不是簡單地作為C#中巢狀查詢表示式,而是將查詢表示式作為C#的一種語法。
在.NET類庫中,LINQ相關類庫都在System.Linq名稱空間下,該名稱空間提供支援使用LINQ進行查詢的類和介面,其中最主要的是以下兩個類和兩個介面。
※IEnumerable介面:它表示可以查詢的資料集合,一個查詢通常是逐個對集合中的元素進行篩選操作,返回一個新的IEnumerable介面,用來儲存查詢結果。
※IQueryable介面:他繼承IEnumerable介面,表示一個可以查詢的表示式目錄樹。
※Enumerable類:它通過對IEnumerable提供擴充套件方法,實現LINQ標準查詢運算子。包括過路、導航、排序、查詢、聯接、求和、求最大值、求最小值等操作。
※Queryable類:它通過對IQueryable提供擴充套件方法,實現LINQ標準查詢運算子。包括過路、導航、排序、查詢、聯接、求和、求最大值、求最小值等操作。
Lambda表示式實際上是一個匿名函式,它可以說是對LINQ的補充。由於LINQ查詢關鍵字和IEnumerable介面的方法之間有一個對應關係,但是LINQ查詢表示式中可以使用的查詢功能很少。在實際開發中通過查詢結果或資料來源進行方法呼叫,從而進行更多的查詢操作。由於Lambda表示式是匿名函式,它可以賦值到一個委託,而在IEnumerable介面的方法中很多通過函式委託來實現自定義運算、條件等操作,所以Lambda表示式在LINQ中被廣泛使用。

使用方法

※查詢全部內容

1 查詢Student表的所有記錄。
2 select * from student
3 Linq:
4     from s in Students
5     select s
6 Lambda:
7     Students.Select( s => s) 

※按列查詢

select sname,ssex,class from student
 3 Linq:
 4     from s in Students
 5     select new {
 6         s.SNAME,
 7         s.SSEX,
 8         s.CLASS
 9     }
10 Lambda:
11     Students.Select( s => new {
12         SNAME = s.SNAME,SSEX = s.SSEX,CLASS = s.CLASS
13     })

※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) 

※兩個區間內查詢

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     )

※在一個範圍內查詢

 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))

※或關係查詢

 查詢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 == "女")) 

※排序

以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)

※行數查詢

 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()

※平均值查詢

查詢'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)

※潛逃查詢

查詢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()

※分組

查詢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") 

※分組過濾

查詢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") 

※多表聯合查詢

 select sc.sno,c.cname,sc.degree from course as c,score as sc where c.cno = sc.cno
 2 Linq:
 3     from c in Courses
 4     join sc in Scores
 5     on c.CNO equals sc.CNO
 6     select new
 7     {
 8         sc.SNO,c.CNAME,sc.DEGREE
 9     }
10 Lambda:
11     Courses.Join ( Scores, c => c.CNO,
12                              sc => sc.CNO,
13                              (c, sc) => new
14                                         {
15                                             SNO = sc.SNO,
16                                             CNAME = c.CNAME,
17                                             DEGREE = sc.DEGREE
18                                         })
19                 .Average()