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

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

image

原覺得必須實現 IQueryable 才能夠實現,結果一次驚喜,原來只要有對應的方法就成。sql

雖然支持了,可是仍是推薦使用【鏈式 + lambda】 !!!編碼

特別說明

此次功能更新,ISelect 增長了 5個方法,對【鏈式 + lambda】的用戶可能會形成少量影響,我在註釋上標明瞭,以下圖:code

image

特別是 .Select(),原先沒有支持,該功能與 ToList(a => new Dto{}) 合併實現的。htm

須要避免一下坑:blog

  • 若是必定要使用 .Select() 方法,請務必在 .ToList() 以前調用它;繼承

  • 請減小圖中方法在【鏈式 + labmda】模式下的使用;事務

全部 ISelect 均可以使用 linq to sql,包括 Repository、DbContext;get

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();

系列文章導航

相關文章
相關標籤/搜索