原本不支持 IQueryable 主要出于使用習慣的考慮,如果繼承 IQueryable,編寫代碼的智能總會提示出現一堆你不想使用的方法(對不起,我有強迫癥),IQueryable 自身提供了一堆沒法實現的方法,還有外部入侵的擴展方法,嚴重影響編碼體驗。如下圖:
原以為必須實現 IQueryable 才可以實現,結果一次驚喜,原來只要有對應的方法就成。
雖然支持了,但是還是推薦使用【鏈式 + lambda】 !!!
特別說明
這次功能更新,ISelect 增加了 5個方法,對【鏈式 + lambda】的用戶可能會造成少許影響,我在注釋上標明了,如下圖:
特別是 .Select(),原先沒有支持,該功能與 ToList(a => new Dto{}) 合并實現的。
需要避免一下坑:
如果一定要使用 .Select() 方法,請務必在 .ToList() 之前調用它;
請減少圖中方法在【鏈式 + 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(多表查詢)
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();
系列文章導航
(一)入門
(二)自動遷移實體
(三)實體特性
(四)實體特性 Fluent Api
(五)插入數據
(六)批量插入數據
(七)插入數據時忽略列
(八)插入數據時指定列
(九)刪除數據
(十)更新數據
(十一)更新數據 Where
(十二)更新數據時指定列
(十三)更新數據時忽略列
(十四)批量更新數據
(十五)查詢數據
(十六)分頁查詢
(十七)聯表查詢
(十八)導航屬性
(十九)多表查詢
(二十)多表查詢 WhereCascade
(二十一)查詢返回數據
(二十二)Dto 映射查詢
(二十三)分組、聚合
(二十四)Linq To Sql 語法使用介紹
(二十五)延時加載
(二十六)貪婪加載 Include、IncludeMany、Dto、ToList
(二十七)將已寫好的 SQL 語句,與實體類映射進行二次查詢
(二十八)事務
(二十九)Lambda 表達式
(三十)讀寫分離
(三十一)分區分表
(三十二)Aop
(三十三)CodeFirst 類型映射
(三十四)CodeFirst 遷移說明
(三十五)CodeFirst 自定義特性