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 ...
随机推荐
- C++在线编程网站
1.推荐 http://www.dooccn.com/cpp/ 2.https://wandbox.org/ 3.https://www.tutorialspoint.com/compile_cpp_ ...
- AndroidStudio制作Nine-Patch【.9】图片
使用AndroidStudio制作Nine-Patch[.9]图片,以及为什么要制作Nine-Patch[.9]图片[以聊天气泡为例] 本文链接:https://blog.csdn.net/She ...
- ISO/IEC 9899:2011 条款6.2.6——类型的表示
6.2.6 类型的表示 6.2.6.1 通用类型 1.所有类型的表示都是未指定的,除了在本小节所描述的之外. 2.除了位域(bit-field),对象由连续的一个或多个字节序列构成,这些字节序列的字节 ...
- ehcache的heap、off-heap、desk浅谈
ehcache的heap.off-heap.desk浅谈 答: 从读取速度上比较:heap > off-heap > disk heap堆内内存: heap表示使用堆内内存,heap( ...
- List根据多个字段分组
List<ClassEntity> distinctClass = classEntities.stream().collect(Collectors.collectingAndThen( ...
- 使用select和show命令查看mysql数据库系统信息
(1).select 显示当前日期和时间 mysql> select now(); +---------------------+ | now() | +-------------------- ...
- iframe重定向让父页面跳转
情景描述 我们在使用一些后台程序的html模板(比如H-ui)的时候,这些html前端程序是iframe版的, 也就是说在使用的时候,每当我点击左侧导航栏的一个按钮,在右侧就会弹出一个菜单栏,在显示的 ...
- page工具类
工具类 /** * @Title: PageUtil.java * @Package * @Description: TODO(用一句话描述该文件做什么) * @author licy * @date ...
- c# 如何给 dataGridView里添加一个自增长列(列名为序号)
System.Data.DataTable table = new DataTable(); System.Data.DataColumn column = new Da ...
- 逆天的flexbox布局
Flexbox是spankin新推出的一种CSS布局模块,拥有完美的浏览器兼容性!它可以轻易做到垂直居中.重新排序.布局的动态伸展与收缩. Flexbox兼容性参考 点击查看基本教程介绍(请用电脑上的 ...