原本不支持 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. Linux:oracle11.2.0dbca静默建库

    1.关闭防火墙 systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall ...

  2. 【译】为什么要了解HTTP

    原文地址:Why should I care about HTTP? 原作信息:by Devon Campbell. Dec 15 '18 Originally published at raddev ...

  3. Ubuntu 17 安装Chrome浏览器

    1.进入下载文件存放目录 cd Downloads 2.下载chrome文件 2.1 32位使用如下命令 wget https://dl.google.com/linux/direct/google- ...

  4. python代码规范整理

    规范参考源: 1.pep8(python代码样式规范):中文文档      https://blog.csdn.net/ratsniper/article/details/78954852 2.pep ...

  5. 2019Hexo博客Next主题深度美化 打造一个炫酷博客(2)-奥怪的小栈

    219/8/1 更新 本文转载于:奥怪的小栈 这篇文章告诉你在搭建好博客后,面对网上千篇一律的美化教程怎么才能添加自己独特点,使人眼前一亮. 本站基于HEXO+Github搭建. 所以你需要准备好HE ...

  6. 基于STM32F429和Cube的ov2640程序

    1.ov2640和DCMI介绍 OV2640 是 OV(OmniVision)公司生产的一颗 1/4 寸的 CMOS UXGA(1632*1232)图 像传感器.该传感器体积小.工作电压低,提供单片 ...

  7. mybatis嵌套map或者map嵌套的parameterType

    Spring的重要注解 https://www.cnblogs.com/rolandlee/p/11014923.html 一:首先是map嵌套: 例1: 例2: 总结: paramterType无论 ...

  8. Egret白鹭开发微信小游戏排行榜功能

    推荐阅读: 我的CSDN 我的博客园 QQ群:704621321 我的个人博客 最近事情特别多,今天终于实现了排行榜功能,记录下来大家一起学习学习. 一.调用默认排行榜 首先我们需要了解: 1.白鹭开 ...

  9. mysql 实战

    建表语句: CREATE TABLE employee ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(150) NOT NULL DEFAULT ...

  10. filter修改post参数

    前景:公司项目web渗透测试中提出管理登录时,传输密码不能为明文,需要加密传输,但是迫于系统架构,后端代码不能修改,只能在filter中解密参数. 1.前端加密处理: <script type= ...