Skip to content

LinqToSql

28810 edited this page May 7, 2019 · 13 revisions

不支持 IQueryable 主要出于使用习惯的优化考虑,因为当编写代码时,智能提示会出现一堆你不想使用的方法,如下图:

image

FreeSql 没想过支持 linq to sql 语法,原以为必须实现 IQueryable 才可以实现,结果一次惊喜,原来 ISelect 也行。

linq to sql 不需要实现 IQueryable,只要有对应的方法就成。

虽然支持了,但是还请使用 链式 + lambda 吧!!!

特别说明

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

image

特别是 .Select() 方法,原先是没有支持了,这个功能放在了 ToList(a => new Dto{}) 合并来使用的。现在也可以使用 .Select(),需要避免一下坑:

  • 请务必在 .ToList() 之前使用 .Select();

  • 请减少以上方法在【链式 + labmda】的模式下调用;

所有 ISelect 都可以使用类似以下的方法,包括 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();

参考资料

Clone this wiki locally