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

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

系列文章导航

FreeSql (二十四)Linq To Sql 语法使用介绍的更多相关文章

  1. FreeSql (十四)批量更新数据

    FreeSql支持丰富的更新数据方法,支持单条或批量更新,在特定的数据库执行还可以返回更新后的记录值. var connstr = "Data Source=127.0.0.1;Port=3 ...

  2. [LINQ2Dapper]最完整Dapper To Linq框架(四)---Linq和SQL并行使用

    目录 [LINQ2Dapper]最完整Dapper To Linq框架(一)---基础查询 [LINQ2Dapper]最完整Dapper To Linq框架(二)---动态化查询 [LINQ2Dapp ...

  3. 学习笔记:CentOS7学习之二十四:expect-正则表达式-sed-cut的使用

    目录 学习笔记:CentOS7学习之二十四:expect-正则表达式-sed-cut的使用 24.1 expect实现无交互登录 24.1.1 安装和使用expect 24.2 正则表达式的使用 24 ...

  4. Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等)

    Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等) 子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 = from c i ...

  5. Bootstrap<基础二十四> 缩略图

    Bootstrap 缩略图.大多数站点都需要在网格中布局图像.视频.文本等.Bootstrap 通过缩略图为此提供了一种简便的方式.使用 Bootstrap 创建缩略图的步骤如下: 在图像周围添加带有 ...

  6. 二十四、Struts2中的UI标签

    二十四.Struts2中的UI标签 Struts2中UI标签的优势: 数据回显 页面布局和排版(Freemark),struts2提供了一些常用的排版(主题:xhtml默认 simple ajax) ...

  7. WCF技术剖析之二十四: ServiceDebugBehavior服务行为是如何实现异常的传播的?

    原文:WCF技术剖析之二十四: ServiceDebugBehavior服务行为是如何实现异常的传播的? 服务端只有抛出FaultException异常才能被正常地序列化成Fault消息,并实现向客户 ...

  8. VMware vSphere 服务器虚拟化之二十四 桌面虚拟化之手动池管理物理机

    VMware vSphere 服务器虚拟化之二十四 桌面虚拟化之手动池管理物理机 VMwareView手动池可以管理物理计算机 说明: 环境基于实验二十三 1.准备一台Windows 7的物理计算机名 ...

  9. Bootstrap入门(二十四)data属性

    Bootstrap入门(二十四)data属性 你可以仅仅通过 data 属性 API 就能使用所有的 Bootstrap 插件,无需写一行 JavaScript 代码.这是 Bootstrap 中的一 ...

随机推荐

  1. rpm包制作介绍

    RPM(Rpm Package Management)在ReadHat等发行版下被用作软件包管理程序,其将某个软件相关的文件置入一个.rpm包中,用rpm命令,我们可以方便地完成Linux下软件安装. ...

  2. 洛谷 P2572 [SCOI2010]序列操作

    题意简述 维护一个序列,支持如下操作 把[a, b]区间内的所有数全变成0 把[a, b]区间内的所有数全变成1 把[a,b]区间内所有的0变成1,所有的1变成0 询问[a, b]区间内总共有多少个1 ...

  3. k8s学习笔记

    9.deployment:声明式的升级应用 9.1.使用RC实现滚动升级 #kubectl rolling-update kubia-v1 kubia-v2 --image=luksa/kubia:v ...

  4. CheckListBox怎样得到多选值?

    一般认为:foreach (object obj in checkedListBox1.SelectedItems)即可遍历选中的值.其实这里遍历的只是高亮的值并不是打勾的值.遍历打勾的值要用下面的代 ...

  5. 数据库炸了——是谁动了我的wait_timeout

    1.起因 隐约听到坐在我对面的测试说测试环境的接口有问题 他们一番商讨后,朝我这边反馈说,现在测试环境的接口报504 我条件反射的回了句那是接口超时,再多试几次(测试环境的性能比较差,尤其是数据库,经 ...

  6. DRF (Django REST framework) 中的视图类

    视图说明 1. 两个基类 1)APIView rest_framework.views.APIView APIView是REST framework提供的所有视图的基类,继承自Django的View父 ...

  7. LoRaWAN_stack移植笔记(四)__RTC

    stm32相关的配置 由于例程使用的主控芯片为STM32L151C8T6,而在本设计中使用的主控芯片为STM32L051C8T6,内核不一样,并且Cube库相关的函数接口及配置也会有不同,所以芯片的驱 ...

  8. 基于CAS分析对ABA问题的一点思考

    基于CAS分析对ABA问题的一点思考 什么是CAS? 背景 synchronized加锁消耗太大 volatile只保证可见性,不保证原子性 基础 用CPU提供的特殊指令,可以: 自动更新共享数据; ...

  9. 第五章 函数day2

    5.2函数小高级 5.2.1 函数当参数 1 函数也可以当返回值 def v(dar): v = dar() def n (): print(444) v(n) # 实例2 def v(): prin ...

  10. 设计模式(C#)——02抽象工厂模式

    推荐阅读:  我的CSDN  我的博客园  QQ群:704621321       在工厂模式中,一个工厂只能创建一种产品,但我们往往希望,一个工厂能创建一系列产品.很明显工厂模式已经不能满足我们的需 ...