FreeSql (二十四)Linq To Sql 語法使用介紹


原本不支持 IQueryable 主要出於使用習慣的考慮,編寫代碼的智能總會提示出現一堆你不想使用的方法(對不起,我有強迫症),IQueryable 自身提供了一堆沒法實現的方法,還有外部入侵的擴展方法,嚴重影響編碼體驗。如下圖:

v1.4.0+ 版本請使用以下命令安裝(老版本不需要安裝):

dotnet add package FreeSql.Extensions.Linq

特別說明

  • 請盡量不要在 ISelect 模式下的使用 Linq 方法:GroupJoin、Select、SelectMany、Join、DefaultIfEmpty;

  • 如果一定要在 ISelect 中使用 .Select() 方法,請務必在 .ToList() 之前調用它;

IQueryable

FreeSql 提供強大的數據查詢對象 ISelect。

FreeSql.Extensions.Linq v1.4.0+ 實現了 IQueryable 查詢對象常用功能,以便在各框架中交互使用。

//將 ISelect 轉為 IQueryable
IQueryable<Student> queryable = fsql.Select<Student>().AsQueryable();

//Linq 方法查詢
var t1 = queryable.Where(a => a.id == 1).FirstOrDefault();

//將 IQueryable 還原為 ISelect
ISelect<Studeng> select = queryable.RestoreToSelect();

注意:IQueryable 的實現目前不支持 GroupBy,可以考慮使用 RestoreSelect 方法轉回 ISelect 進行查詢

Where

var t1 = (
    from a in fsql.Select<Student>()
    where a.id == item.id
    select a
).ToList();

Select(指定字段)

var t1 = (
    from a in fsql.Select<Student>()
    where a.id == item.id
    select new { a.id }
).ToList();

CaseWhen

var t1 = (
    from a in fsql.Select<Student>()
    where a.id == item.id
    select 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.StudentId
    select a
).ToList();

var t2 = (
    from a in fsql.Select<Student>()
    join b in fsql.Select<School>() on a.id equals b.StudentId
    select 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.StudentId
    where a.id == item.id
    select 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 temp
    from 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 temp
    from 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 temp
    from tc in temp.DefaultIfEmpty()
    where a.id == item.id
    select 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.StudentId
    select a
).ToList();

var t2 = (
    from a in fsql.Select<Student>()
    from b in fsql.Select<School>()
    where a.id == b.StudentId
    select 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.StudentId
    where a.id == item.id
    select new { a.id, bid = b.id }
).ToList();

GroupBy(分組)

var t1 = (
    from a in fsql.Select<Student>()
    where a.id == item.id
    group a by new {a.id, a.name } into g
    select 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();

系列文章導航


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM