日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發布時間:2024/10/12 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 Studentsselect 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 Studentsselect s Lambda:Students.Select( s => s)4、 查詢Score表中成績在60到80之間的所有記錄。 select * from score where degree between 60 and 80 Linq:from s in Scoreswhere s.DEGREE >= 60 && s.DEGREE < 80select 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: Infrom s in Scoreswhere (new decimal[]{85,86,88}).Contains(s.DEGREE)select s Lambda:Scores.Where( s => new Decimal[] {85,86,88}.Contains(s.DEGREE)) Not infrom s in Scoreswhere !(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)不可,以二個主鍵于與一個主鍵進行Anyfrom e in CustomerDemographicswhere !e.CustomerCustomerDemos.Any()select efrom c in Categorieswhere !c.Products.Any()select c 6、 查詢Student表中"95031"班或性別為"女"的同學記錄。 select * from student where class ='95031' or ssex= N'' Linq:from s in Studentswhere 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 Studentsorderby s.CLASS descendingselect 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 Scoresorderby s.DEGREE descendingorderby 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 Studentswhere 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 Studentsfrom c in Coursesfrom sc in Scoreslet maxDegree = (from sss in Scoresselect sss.DEGREE).Max()let sno = (from ss in Scoreswhere ss.DEGREE == maxDegreeselect ss.SNO).Single().ToString()let cno = (from ssss in Scoreswhere ssss.DEGREE == maxDegreeselect ssss.CNO).Single().ToString()where s.SNO == sno && c.CNO == cnoselect new {s.SNO,c.CNO}).Distinct() 操作時問題?執行時報錯: where s.SNO == sno(這行報出來的) 運算符"=="無法應用于"string"和"System.Linq.IQueryable<string>"類型的操作數 解決: 原:let sno = (from ss in Scoreswhere ss.DEGREE == maxDegreeselect ss.SNO).ToString() Queryable().Single()返回序列的唯一元素;如果該序列并非恰好包含一個元素,則會引發異常。 解:let sno = (from ss in Scoreswhere ss.DEGREE == maxDegreeselect ss.SNO).Single().ToString() 11、查詢'3-105'號課程的平均分。 select avg(degree) from score where cno = '3-105' Linq:(from s in Scoreswhere 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 Scoreswhere s.CNO.StartsWith("3")group s by s.CNOinto ccwhere cc.Count() >= 5select 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 Scoresgroup s by s.SNOinto sswhere ss.Min(cc => cc.DEGREE) > 70 && ss.Max( cc => cc.DEGREE) < 90select 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 Studentsjoin sc in Scoreson s.SNO equals sc.SNOselect 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 Coursesjoin sc in Scoreson c.CNO equals sc.CNOselect 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 Studentsfrom c in Coursesfrom sc in Scoreswhere s.SNO == sc.SNO && c.CNO == sc.CNOselect new { s.SNAME,c.CNAME,sc.DEGREE }

?

轉載于:https://www.cnblogs.com/keepee/p/6120991.html

總結

以上是生活随笔為你收集整理的SQL、LINQ、Lambda 三种用法(转)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。