LinqToSql

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

image

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 查询对象常用功能,以便在各框架中交互使用。

  1. //将 ISelect 转为 IQueryable
  2. IQueryable<Student> queryable = fsql.Select<Student>().AsQueryable();
  3. //Linq 方法查询
  4. var t1 = queryable.Where(a => a.id == 1).FirstOrDefault();
  5. //将 IQueryable 还原为 ISelect
  6. ISelect<Studeng> select = queryable.RestoreToSelect();

注意:IQueryable 的实现目前不支持 GroupBy,可以考虑使用 RestoreSelect 方法转回 ISelect 进行查询

Where

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

Select(指定字段)

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

CaseWhen

  1. var t1 = (
  2. from a in fsql.Select<Student>()
  3. where a.id == item.id
  4. select new {
  5. a.id,
  6. a.name,
  7. testsub = new {
  8. time = a.age > 10 ? "大于" : "小于或等于"
  9. }
  10. }
  11. ).ToList();

Join

  1. var t1 = (
  2. from a in fsql.Select<Student>()
  3. join b in fsql.Select<School>() on a.id equals b.StudentId
  4. select a
  5. ).ToList();
  6. var t2 = (
  7. from a in fsql.Select<Student>()
  8. join b in fsql.Select<School>() on a.id equals b.StudentId
  9. select new { a.id, bid = b.id }
  10. ).ToList();
  11. var t3 = (
  12. from a in fsql.Select<Student>()
  13. join b in fsql.Select<School>() on a.id equals b.StudentId
  14. where a.id == item.id
  15. select new { a.id, bid = b.id }
  16. ).ToList();

LeftJoin

  1. var t1 = (
  2. from a in fsql.Select<Student>()
  3. join b in fsql.Select<School>() on a.id equals b.StudentId into temp
  4. from tc in temp.DefaultIfEmpty()
  5. select a
  6. ).ToList();
  7. var t2 = (
  8. from a in fsql.Select<Student>()
  9. join b in fsql.Select<School>() on a.id equals b.StudentId into temp
  10. from tc in temp.DefaultIfEmpty()
  11. select new { a.id, bid = tc.id }
  12. ).ToList();
  13. var t3 = (
  14. from a in fsql.Select<Student>()
  15. join b in fsql.Select<School>() on a.id equals b.StudentId into temp
  16. from tc in temp.DefaultIfEmpty()
  17. where a.id == item.id
  18. select new { a.id, bid = tc.id }
  19. ).ToList();

From(多表查询)

  1. var t1 = (
  2. from a in fsql.Select<Student>()
  3. from b in fsql.Select<School>()
  4. where a.id == b.StudentId
  5. select a
  6. ).ToList();
  7. var t2 = (
  8. from a in fsql.Select<Student>()
  9. from b in fsql.Select<School>()
  10. where a.id == b.StudentId
  11. select new { a.id, bid = b.id }
  12. ).ToList();
  13. var t3 = (
  14. from a in fsql.Select<Student>()
  15. from b in fsql.Select<School>()
  16. where a.id == b.StudentId
  17. where a.id == item.id
  18. select new { a.id, bid = b.id }
  19. ).ToList();

GroupBy(分组)

  1. var t1 = (
  2. from a in fsql.Select<Student>()
  3. where a.id == item.id
  4. group a by new {a.id, a.name } into g
  5. select new {
  6. g.Key.id, g.Key.name,
  7. cou = g.Count(),
  8. avg = g.Avg(g.Value.age),
  9. sum = g.Sum(g.Value.age),
  10. max = g.Max(g.Value.age),
  11. min = g.Min(g.Value.age)
  12. }
  13. ).ToList();