FreeSql (二十四)Linq To Sql 语法使用介绍
原本不支持 IQueryable 主要出于使用習(xí)慣的考慮,如果繼承 IQueryable,編寫(xiě)代碼的智能總會(huì)提示出現(xiàn)一堆你不想使用的方法(對(duì)不起,我有強(qiáng)迫癥),IQueryable 自身提供了一堆沒(méi)法實(shí)現(xiàn)的方法,還有外部入侵的擴(kuò)展方法,嚴(yán)重影響編碼體驗(yàn)。如下圖:
原以為必須實(shí)現(xiàn) IQueryable 才可以實(shí)現(xiàn),結(jié)果一次驚喜,原來(lái)只要有對(duì)應(yīng)的方法就成。
雖然支持了,但是還是推薦使用【鏈?zhǔn)?+ lambda】 !!!
特別說(shuō)明
這次功能更新,ISelect 增加了 5個(gè)方法,對(duì)【鏈?zhǔn)?+ lambda】的用戶(hù)可能會(huì)造成少許影響,我在注釋上標(biāo)明了,如下圖:
特別是 .Select(),原先沒(méi)有支持,該功能與 ToList(a => new Dto{}) 合并實(shí)現(xiàn)的。
需要避免一下坑:
如果一定要使用 .Select() 方法,請(qǐng)務(wù)必在 .ToList() 之前調(diào)用它;
請(qǐng)減少圖中方法在【鏈?zhǔn)?+ labmda】模式下的使用;
所有 ISelect 都可以使用 linq to sql,包括 Repository、DbContext;
Where
var t1 = (from a in fsql.Select<Student>()where a.id == item.idselect a ).ToList();Select(指定字段)
var t1 = (from a in fsql.Select<Student>()where a.id == item.idselect new { a.id } ).ToList();CaseWhen
var t1 = (from a in fsql.Select<Student>()where a.id == item.idselect new {a.id,a.name,testsub = new {time = a.age > 10 ? "大于" : "小于或等于"}} ).ToList();Join
var t1 = (from a in fsql.Select<Student>()join b in fsql.Select<School>() on a.id equals b.StudentIdselect a ).ToList();var t2 = (from a in fsql.Select<Student>()join b in fsql.Select<School>() on a.id equals b.StudentIdselect new { a.id, bid = b.id } ).ToList();var t3 = (from a in fsql.Select<Student>()join b in fsql.Select<School>() on a.id equals b.StudentIdwhere a.id == item.idselect new { a.id, bid = b.id } ).ToList();LeftJoin
var t1 = (from a in fsql.Select<Student>()join b in fsql.Select<School>() on a.id equals b.StudentId into tempfrom tc in temp.DefaultIfEmpty()select a ).ToList();var t2 = (from a in fsql.Select<Student>()join b in fsql.Select<School>() on a.id equals b.StudentId into tempfrom tc in temp.DefaultIfEmpty()select new { a.id, bid = tc.id } ).ToList();var t3 = (from a in fsql.Select<Student>()join b in fsql.Select<School>() on a.id equals b.StudentId into tempfrom tc in temp.DefaultIfEmpty()where a.id == item.idselect new { a.id, bid = tc.id } ).ToList();From(多表查詢(xún))
var t1 = (from a in fsql.Select<Student>()from b in fsql.Select<School>()where a.id == b.StudentIdselect a ).ToList();var t2 = (from a in fsql.Select<Student>()from b in fsql.Select<School>()where a.id == b.StudentIdselect new { a.id, bid = b.id } ).ToList();var t3 = (from a in fsql.Select<Student>()from b in fsql.Select<School>()where a.id == b.StudentIdwhere a.id == item.idselect new { a.id, bid = b.id } ).ToList();GroupBy(分組)
var t1 = (from a in fsql.Select<Student>()where a.id == item.idgroup a by new {a.id, a.name } into gselect new {g.Key.id, g.Key.name,cou = g.Count(),avg = g.Avg(g.Value.age),sum = g.Sum(g.Value.age),max = g.Max(g.Value.age),min = g.Min(g.Value.age)} ).ToList();系列文章導(dǎo)航
(一)入門(mén)
(二)自動(dòng)遷移實(shí)體
(三)實(shí)體特性
(四)實(shí)體特性 Fluent Api
(五)插入數(shù)據(jù)
(六)批量插入數(shù)據(jù)
(七)插入數(shù)據(jù)時(shí)忽略列
(八)插入數(shù)據(jù)時(shí)指定列
(九)刪除數(shù)據(jù)
(十)更新數(shù)據(jù)
(十一)更新數(shù)據(jù) Where
(十二)更新數(shù)據(jù)時(shí)指定列
(十三)更新數(shù)據(jù)時(shí)忽略列
(十四)批量更新數(shù)據(jù)
(十五)查詢(xún)數(shù)據(jù)
(十六)分頁(yè)查詢(xún)
(十七)聯(lián)表查詢(xún)
(十八)導(dǎo)航屬性
(十九)多表查詢(xún)
(二十)多表查詢(xún) WhereCascade
(二十一)查詢(xún)返回?cái)?shù)據(jù)
(二十二)Dto 映射查詢(xún)
(二十三)分組、聚合
(二十四)Linq To Sql 語(yǔ)法使用介紹
(二十五)延時(shí)加載
(二十六)貪婪加載 Include、IncludeMany、Dto、ToList
(二十七)將已寫(xiě)好的 SQL 語(yǔ)句,與實(shí)體類(lèi)映射進(jìn)行二次查詢(xún)
(二十八)事務(wù)
(二十九)Lambda 表達(dá)式
(三十)讀寫(xiě)分離
(三十一)分區(qū)分表
(三十二)Aop
(三十三)CodeFirst 類(lèi)型映射
(三十四)CodeFirst 遷移說(shuō)明
(三十五)CodeFirst 自定義特性
轉(zhuǎn)載于:https://www.cnblogs.com/FreeSql/p/11531392.html
總結(jié)
以上是生活随笔為你收集整理的FreeSql (二十四)Linq To Sql 语法使用介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: lua52 C API测试代码
- 下一篇: 2013年最值得我们学习的网页作品示例【