FreeSql (二十三)Linq To Sql 语法使用介绍

时间:2019-09-17
本文章向大家介绍FreeSql (二十三)Linq To Sql 语法使用介绍,主要包括FreeSql (二十三)Linq To Sql 语法使用介绍使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

原本不支持 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.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();

原文地址:https://www.cnblogs.com/FreeSql/p/11531384.html