天天看點

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

繼續閱讀