美文网首页.NETdotNET
FreeSql 教程 (二十四)Linq To Sql 语法使用

FreeSql 教程 (二十四)Linq To Sql 语法使用

作者: 叶先生_34e6 | 来源:发表于2020-03-17 19:25 被阅读0次

    FreeSql 以 MIT 开源协议托管于 github:https://github.com/2881099/FreeSql

    原本不支持 IQueryable 主要出于使用习惯的考虑,如果继承 IQueryable,编写代码的智能总会提示出现一堆你不想使用的方法(对不起,我有强迫症),IQueryable 自身提供了一堆没法实现的方法,还有外部入侵的扩展方法,严重影响编码体验。如下图:

    image.png

    原以为必须实现 IQueryable 才可以实现,结果一次惊喜,原来只要有对应的方法就成。

    虽然支持了,但是还是推荐使用【链式 + lambda】 !!!

    特别说明

    这次功能更新,ISelect 增加了 5个方法,对【链式 + lambda】的用户可能会造成少许影响,我在注释上标明了,如下图:

    image.png

    特别是 .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();
    

    系列文章导航

    相关文章

      网友评论

        本文标题:FreeSql 教程 (二十四)Linq To Sql 语法使用

        本文链接:https://www.haomeiwen.com/subject/eejudhtx.html