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

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

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

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 语法使用介绍的更多相关文章

  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. hdu1241 油田计数

    具体思路:求联通块,在"@“的周围进行dfs,使用8个方向向量来代表搜索的方向 贴一下我的主要代码段: int dir[8][2]={{1,1},{-1,-1},{1,-1},{-1,1}, ...

  2. 深入解析Mysql中事务的四大隔离级别及其所解决的读现象

    本文详细介绍四种事务隔离级别,并通过举例的方式说明不同的级别能解决什么样的读现象.并且介绍了在关系型数据库中不同的隔离级别的实现原理. 在DBMS中,事务保证了一个操作序列可以全部都执行或者全部都不执 ...

  3. 身体质量指数BMI

    Solution: 方法一:"Python语言程序设计"(中国大学MOOC平台)的答案 分析:对比两种指标,将共性(相同的区间)和异性(不同的区间)细分.这样两种指标的判断条件(不 ...

  4. turtle绘制图形

    Example1: import turtle as t #初始设置画笔的宽度(size).颜色(color) t.pensize(5) t.pencolor("black") # ...

  5. 云上RDS架构

    概述 越来越多的企业选择上云,最基础的云服务就是IaaS(Infrastructure as a Service)服务,直观理解就是虚拟主机,用户不用再自建机房,自己购买服务器,而是直接向云厂商购买虚 ...

  6. 解决mysql乱码

    总结的几个乱码问题 希望我们全体学员也能够学会总结 java web 很是希望大家能够学好.并且也希望大家能够在学习过程中不段的积累相关的知识点 1.在response中写<meta  http ...

  7. C# - 协变、逆变 看完这篇就懂了

    1. 基本概念 官方:协变和逆变都是术语,前者指能够使用比原始指定的派生类型的派生程度更大(更具体的)的类型,后者指能够使用比原始指定的派生类型的派生程度更小(不太具体的)的类型.[MSDN] 公式: ...

  8. Nacos(九):Nacos集群部署和遇到的问题

    前言 前面的系列文章已经介绍了Nacos的如何接入SpringCloud,以及Nacos的基本使用方式 之前的文章中都是基于单机模式部署进行讲解的,本文对Nacos的集群部署方式进行说明 环境准备 J ...

  9. PHP 与 MySQL 相关操作

    一.MySQL基操 •MySQL启动 注意:MySQL不能直接通过 mysql.exe 命令启动 MySQL客户端访问服务端需要寻找匹配:连接认证 连接:IP和端口确定,如果是本地都可以忽略 -h 主 ...

  10. Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III)

    Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III) 深度优先搜索的解题详细介绍,点击 在二维网格 grid 上,有 4 种类型的方格: 1 ...