Linq To Sql的各种查询
SQL 查询 to 各种 LinQ
2023-09-11 14:21:36 时间
一、Inner Join
//request为查询条件
var result = from a in db.TableA
join b in db.TableB
on a.ID equals b.ID
where a.UserName == request.UserName || a.Mobile == request.Mobile
select new ResponseInfo
{
UserName = ui.UserName,
UserID = ui.ID,
Mobile = ui.Mobile,
Value = ue.Value
};
二、Left Join
//request为查询条件
var result = from si in db.TableA
join ai in db.TableB on si.UserID equals ai.ID into ljTableB
from ai in ljTableB.DefaultIfEmpty()
join pi in db.TableC on si.ProductID equals pi.ID into ljTableC
from pi in ljTableC.DefaultIfEmpty()
join ps in db.TableD on si.SpecID equals ps.ID into ljTableD
from ps in ljTableD.DefaultIfEmpty()
select new InfoResposne
{
ID = si.ID,
DisplayID = pi.DisplayID,
ProductID = si.ProductID,
ProductName = pi.ProductName,
BarCode = pi.BarCode,
SpecID = si.SpecID,
Created = si.Created
};
二、动态添加查询条件及翻页
//字符串
if (!string.IsNullOrWhiteSpace(request.DisplayID))
{
result = result.Where(p => p.DisplayID == request.DisplayID);
}
//字符串包含
if (!string.IsNullOrWhiteSpace(request.UserName))
{
userInfo = userInfo.Where(p => p.UserName.Contains(request.UserName));
}
//数字、状态类型
if (request.Amount!=null)
{
result = result.Where(p => p.Amount == request.Amount);
}
//时间范围
if (request.StockInStart != null)
{
result = result.Where(p => p.Created >= request.StockInStart);
}
//时间范围
if (request.StockInEnd != null)
{
result = result.Where(p => p.Created <= request.StockInEnd);
}
var response = new LogResponse();
response.TotalCount = result.Count();
response.DataList = result
.OrderByDescending(p => p.Created)
.Skip((request.PageIndex - 1) * request.PageSize)
.Take(request.PageSize)
.ToList();
相关文章
- 【SQL 学习】LEVEL 伪列
- 【SQL 学习】层次化查询之CONNECT BY 和 START WITH
- sql server递归查询
- sql server简单查询
- 一个小面试题sql
- Hive sql 查询数据库查询 top-n
- 如何查找MySQL中查询慢的SQL语句
- 在SQL 2005中用T-SQL插入中文数据时出现的问号或乱码的解决方案[转]
- 【Teradata SQL】数据库中查询 一个字段包含另一个字段sql
- SQL Server调优系列基础篇
- 查询SQL存储过程创建时间
- 两个sql查询语句之间的左连接left join
- 使用Spark SQL构建交互式查询引擎
- 【学习总结】SQL的学习-2-sql操作
- sql 语句系列(用魔法打败魔法)[八百章之第十章]
- 好用的SQL工具盘点:从学习到工作总有一款适合你
- SQL 宽字节注入详解
- SQL语句详解(一)——基本增删改操作
- Sql Server生成测试数据
- SQL Server 2008 R2 新建用户并指定该用户的数据库
- Oracle与Sql Server复制表结构及数据
- Python编程:records库操作SQL查询MySQL数据库
- Hibernate的四种查询方式(主键查询,HQL查询,Criteria查询,本地sql查询)和修改和添加
- SQL中Case的使用方法(下篇)(转)
- 基于ODPS的SQL语句