Linq to sql之left join运用示例
示例一:
var l= from a in cardsBll.GetCards()
join b in usersBll.GetAllUsers()
on a.CardSn equals b.CardSn into temp
where a.CardSn.Contains(key)
from t in temp.DefaultIfEmpty()
select new CardsAndUsersView
{
Card_Id = a.Id,
CardPw = a.CardPw,
Expries = a.Expries,
IsEnabled = a.IsEnabled,
CardSn = a.CardSn,
CreateDate = a.CreateDate,
ModifyDate = a.ModifyDate,
User_Id = t==null?0:t.Id,
Province = t == null ? "" : t.Province,
City = t == null ? "" :t.City,
AreaOrCounty = t == null ? "" : t.AreaOrCounty,
AddressInfo = t == null ? "" : t.AddressInfo
};
生成的SQL如下:
exec sp_executesql N'SELECT TOP (20)
[Project1].[Id] AS [Id],
[Project1].[CardPw] AS [CardPw],
[Project1].[Expries] AS [Expries],
[Project1].[IsEnabled] AS [IsEnabled],
[Project1].[CardSn] AS [CardSn],
[Project1].[CreateDate] AS [CreateDate],
[Project1].[ModifyDate] AS [ModifyDate],
[Project1].[C1] AS [C1],
[Project1].[C2] AS [C2],
[Project1].[C3] AS [C3],
[Project1].[C4] AS [C4],
[Project1].[C5] AS [C5]
FROM ( SELECT [Project1].[Id] AS [Id], [Project1].[CardSn] AS [CardSn], [Project1].[CardPw] AS [CardPw], [Project1].[IsEnabled] AS [IsEnabled], [Project1].[Expries] AS [Expries], [Project1].[CreateDate] AS [CreateDate], [Project1].[ModifyDate] AS [ModifyDate], [Project1].[C1] AS [C1], [Project1].[C2] AS [C2], [Project1].[C3] AS [C3], [Project1].[C4] AS [C4], [Project1].[C5] AS [C5], row_number() OVER (ORDER BY [Project1].[Id] ASC) AS [row_number]
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Extent1].[CardSn] AS [CardSn],
[Extent1].[CardPw] AS [CardPw],
[Extent1].[IsEnabled] AS [IsEnabled],
[Extent1].[Expries] AS [Expries],
[Extent1].[CreateDate] AS [CreateDate],
[Extent1].[ModifyDate] AS [ModifyDate],
CASE WHEN ([Extent2].[Id] IS NULL) THEN 0 ELSE [Extent2].[Id] END AS [C1],
CASE WHEN ([Extent2].[Id] IS NULL) THEN N'''' ELSE [Extent2].[Province] END AS [C2],
CASE WHEN ([Extent2].[Id] IS NULL) THEN N'''' ELSE [Extent2].[City] END AS [C3],
CASE WHEN ([Extent2].[Id] IS NULL) THEN N'''' ELSE [Extent2].[AreaOrCounty] END AS [C4],
CASE WHEN ([Extent2].[Id] IS NULL) THEN N'''' ELSE [Extent2].[AddressInfo] END AS [C5]
FROM [dbo].[Cards] AS [Extent1]
LEFT OUTER JOIN [dbo].[UserInfo] AS [Extent2] ON ([Extent1].[CardSn] = [Extent2].[CardSn]) OR (([Extent1].[CardSn] IS NULL) AND ([Extent2].[CardSn] IS NULL))
WHERE (1 = [Extent1].[IsEnabled]) AND ([Extent1].[CardSn] LIKE @p__linq__0 ESCAPE N''~'')
) AS [Project1]
) AS [Project1]
WHERE [Project1].[row_number] > 0
ORDER BY [Project1].[Id] ASC',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'%%'
执行结果:
页面效果:
示例二:
DataClasses1DataContext db = new DataClasses1DataContext();
var leftJoinSql = from student in db.Student
join book in db.Book on student.ID equals book.StudentID into temp
from tt in temp.DefaultIfEmpty()
select new
{
sname= student.Name,
bname = tt==null?"":tt.Name
};
Linq to sql之left join运用示例的更多相关文章
- LINQ TO SQL 中的join(转帖)
http://www.cnblogs.com/ASPNET2008/archive/2008/12/21/1358152.html join对于喜欢写SQL的朋友来说还是比较实用,也比较容易接受的东西 ...
- LINQ to SQL语句之Join和Order By
Join操作 适用场景:在我们表关系中有一对一关系,一对多关系,多对多关系等.对各个表之间的关系,就用这些实现对多个表的操作. 说明:在Join操作中,分别为Join(Join查询), SelectM ...
- Linq To SQL LEFT OUTER JOIN (Left Join)
SQL: SELECT [t0].[ProductName], [t1].[TotalPrice] AS [TotalPrice] FROM [Product] AS [t0] LEFT OUTER ...
- LINQ体验(6)——LINQ to SQL语句之Join和Order By
Join操作 适用场景:在我们表关系中有一对一关系,一对多关系.多对多关系等.对各个表之间的关系,就用这些实现对多个表的操作. 说明:在Join操作中.分别为Join(Join查询), SelectM ...
- Linq to SQL 参考Demo
LINQ to SQL语句()之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句 ...
- Linq to SQL各种参考
原文:https://www.cnblogs.com/lyj/archive/2008/01/23/1049686.htmlhttps://www.cnblogs.com/lyj/archive/20 ...
- Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等)
Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等) 子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 = from c i ...
- LINQ to SQL Count/Sum/Min/Max/Avg Join
public class Linq { MXSICEDataContext Db = new MXSICEDataContext(); // LINQ to SQL // Count/Sum/Min/ ...
- Linq To Sql 语法 子查询 & In & Join
子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 =from cin ctx.Customers where ...
随机推荐
- pycharm 中文乱码
新机的pycharm首次输出中文竟然在控制台报错了,着实让我头疼了一下午 我用的PyCharm是2018.3版本 在调用os.system()的过程中遇到了控制台中文乱码的问题,具体如下 找到对应 ...
- Ionic4.x 中的 UI 组件(UI Components) Slides 轮播图组件、Searchbar 组件、 Segment 组件
Slides 轮播图组件 Ionic4.x 中的轮播图组件是基于 swiper 插件,所以配置 slides 的属性需要在 swiper 的 api 中 找 Swiper Api:http://ida ...
- 使用C语言 判断当前网络是否联通
方式一: int GetNetStat( ) { char buffer[BUFSIZ]; FILE *read_fp; int chars_read; int ret; try { memset( ...
- Qt KDChart编译
最近开发中需要用到甘特图,感觉KDChart这个插件不错,在这里记录一下编译过程(其实很好编译,而且一次性就过了) 下载,kdchart-2.6.1-source,解压 打开src目录,用Qt Cre ...
- python基础之线程、进程、协程
线程 线程基础知识 一个应用程序,可以多进程.也可以多线程. 一个python脚本,默认是单进程,单线程的. I/O操作(音频.视频.显卡操作),不占用CPU,所以: 对于I/O密集型操作,不会占用C ...
- javascript模拟生成uuid
function guid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r ...
- Laravel 项目开发环境配置
1.首先安装Laravel 依赖管理包工具 Composer (前提是本地装好了PHP php -v) php -r "copy('https://install.phpcomposer. ...
- 【Leetcode_easy】643. Maximum Average Subarray I
problem 643. Maximum Average Subarray I 题意:一定长度的子数组的最大平均值. solution1:计算子数组之后的常用方法是建立累加数组,然后再计算任意一定长度 ...
- Turbine聚合https微服务
- Hystrix多个线程池切换执行超时带来的问题(图解)
线程池切换带来的超时问题 上图有什么问题: Controller的Hystrx线程池已经到了超时时间,而FeignClient的Hystrx线程池还没到超时时间. 场景: Controller ...