转载: C#: Left outer joins with LINQ
I always considered Left Outer Join in LINQ to be complex until today when I had to use it in my application. I googled and the firstresult gave a very nice explanation. The only difference between ordinary joins (inner joins) and left joins in LINQ is the use of “join into” and “DefaultIfEmpty()” expressions.
Consider this very simple query (Assuming a scenario that not all the TimesheetLines are associated with a Job)
|
1
2
3
|
Select TL.EntryDate, TL.Hours, J.JobNameFrom TimeSheetLines TLLeft Join Jobs J on TL.JobNo=J.JobNo |
A LINQ query using inner join is
|
1
2
3
4
5
6
7
8
9
10
11
|
var lines = from tl in db.TimeSheetLines join j in db.Jobs on tl.JobNo equals j.JobNo where tl.ResourceNo == resourceNo select new { EntryDate = tl.EntryDate, Hours = tl.Hours, Job = j.JobName }; |
And a LINQ query performing left join is
|
1
2
3
4
5
6
7
8
9
10
11
12
|
var lines = from tl in db.TimeSheetLines join j in db.Jobs on tl.JobNo equals j.JobNo into tl_j where tl.ResourceNo == resourceNo from j in tl_j.DefaultIfEmpty() select new { EntryDate = tl.EntryDate, Hours = tl.Hours, Job = j.JobName }; |
Notice that the only difference is the use of “into” with the join statement followed by reselecting the result using “DefaultIfEmpty()” expression. And here’s the generated SQL for the above LINQ expression.
|
1
2
3
4
|
SELECT [t0].[EntryDate] as [EntryDate], [t0].[Hours] as [Hours], [t1].[JobName] AS [Job]FROM [dbo].[TimeSheetLine] AS [t0]LEFT OUTER JOIN [dbo].[Jobs] AS [t1] ON [t0].[JobNo] = [t1].[JobNo]WHERE [t0].[ResourceNo] = @p0 |
Another LINQ version which is more compact is:
|
1
2
3
4
5
6
7
8
9
|
var lines = from tl in db.TimeSheetLines from j in db.Jobs.Where(j=>j.JobNo == tl.JobNo).DefaultIfEmpty() select new { EntryDate = tl.EntryDate, Hours = tl.Hours, Job = j.JobName }; |
Similarly, this concept can be expanded for multiple left joins. Assuming that a TimeSheetLine will either have a JobNo or an IndirectCode, consider this SQL query:
|
1
2
3
4
|
Select TL.EntryDate, TL.Hours, J.JobName, I.IndirectNameFrom TimeSheetLines TLLeft Join Jobs J on TL.JobNo=J.JobNoLeft Join Indirects I on TL.IndirectCode=I.IndirectCode |
The equivalent LINQ query is:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
var lines = from tl in db.TimeSheetLines join j in db.Jobs on tl.JobNo equals j.JobNo into tl_j join i in db.Indirects on tl.IndirectCode equals i.IndirectCode into tl_i where tl.ResourceNo == resourceNo from j in tl_j.DefaultIfEmpty() from i in tl_i.DefaultIfEmpty() select new { EntryDate = tl.EntryDate, Hours = tl.Hours, Job = j.JobName, Indirect = i.IndirectName, }; |
And the generated SQL is:
|
1
2
3
4
|
SELECT [t0].[EntryDate] as [EntryDate], [t0].[Hours] as [Hours], [t1].[JobName] AS [Job], [t2].[IndirectName] As [Indirect]LEFT OUTER JOIN [dbo].[Jobs] AS [t1] ON [t0].[JobNo] = [t1].[JobNo]LEFT OUTER JOIN [dbo].[Indirects] AS [t2] ON [t0].[IndirectCode] = [t2].[IndirectCode]WHERE [t0].[ResourceNo] = @p0 |
That’s all, left outer joins in LINQ are as easy as in T-SQL. Happy joining.
Update:
Notice that this post describes the approach to perform a Left Outer Join in LINQ To SQL as well as Entity Framework (version 4). The same is not true for Entity Framework version 3.5 since it does not support the DefaultIfEmpty keyword. To perform Left Outer Joins with Entity Framework 3.5, we need to create appropriate relationships (e.g 0..1 to 0..Many) in our Entity Model and they will be automatically translated into TSQL’s Left Join clause.
come form: https://smehrozalam.wordpress.com/2009/06/10/c-left-outer-joins-with-linq/
转载: C#: Left outer joins with LINQ的更多相关文章
- Lerning Entity Framework 6 ------ Joins and Left outer Joins
Joins allow developers to combine data from multiple tables into a sigle query. Let's have a look at ...
- How Distributed Outer Joins on PostgreSQL with Citus Work
转自: https://docs.citusdata.com/en/v7.5/articles/outer_joins.html SQL is a very powerful language for ...
- [转载代码]VB.NET 中查询 Linq to SQL 执行时的SQL语句
在搜索使用LINQ TO SQL 添加数据后获得自增长ID的方法时,发现C#可以使用DebuggerWritter把使用Linq to SQL执行的SQL语句显示到即时窗口,于是在网上搜索到在VB.N ...
- Mysql 5.6 新特性(转载)
本文转载自 http://blog.csdn.net/wulantian/article/details/29593803 感谢主人的辛苦整理 一,安全提高 1.提供保存加密认证信息的方法,使用.my ...
- Hive学习笔记【转载】
本文转载自:http://blog.csdn.net/haojun186/article/details/7977565 1. HIVE结构 Hive 是建立在 Hadoop 上的数据仓库基础构架. ...
- LINQ 常规实践总结
1.Linq 执行多列排序 OrderBy的意义是按照指定顺序排序,连续两次OrderBy,后面一个有可能会打乱前面一个的排序顺序,可能与预期不符. 要实现sql中的order by word,nam ...
- linq用法整理
linq用法整理 普通查询 var highScores = from student in students where student.ExamScores[exam] > score se ...
- LINQ之路15:LINQ Operators之元素运算符、集合方法、量词方法
本篇继续LINQ Operators的介绍,包括元素运算符/Element Operators.集合方法/Aggregation.量词/Quantifiers Methods.元素运算符从一个sequ ...
- LINQ之路13:LINQ Operators之连接(Joining)
Joining IEnumerable<TOuter>, IEnumerable<TInner>→IEnumerable<TResult> Operator 说明 ...
随机推荐
- 组合控件 圆环 ring
使用 可以设置内部填充样式及大小 可以设置边框颜色及宽度 这里只是介绍了其中一种实现方式,其实这种类型的东西完全可以用自定义View去实现,他就是一个空中的大圆+一个空中或实心的小圆,实现起来也是非常 ...
- python的按位运算
#coding=utf-8#"&"按位与运算,是指一个数字转化为二进制,然后这些二进制的数按位来进行与运算a=7&18print a'''首先将7转化为二进制,得到 ...
- python面对对象编程------3:写集合类的三种方法
写一个集合类的三种方法:wrap,extend,invent 一:包装一个集合类 class Deck: def __init__( self ): self._cards = [card6(r+1, ...
- maven第三章 maven使用入门
3.1编写pom groupId 一个项目名称 artifactId 子项目(模块)名称 version 开发中的版本,稳定的版本 name 项目名称,方便信息交流 http://news.cnblo ...
- DNS负载均衡
1)DNS负载均衡的介绍 对于负载均衡的一个典型应用就是DNS负载均衡.庞大的网络地址和网络域名绝对是负载均衡体现优势的地方.那么它的具体原理是如何的呢?本文就将为大家详细介绍一下相关内容. DNS负 ...
- android获取在res文件下的图片资源
//得到该图片的id(name 是该图片的名字,"drawable" 是该图片存放的目录,getPackageName()是应用程序的包) int resID = getResou ...
- Core Data 学习简单整理01
Core Data是苹果针对Mac和iOS平台开发的一个框架, 通过CoreData可以在本地生成数据库sqlite,提供了ORM的功能,将对象和数据模型相互转换 . 通过Core Data管理和操作 ...
- 常用命令常用sql:SHOWVARIABLESLIKE'character%'
mysql学习笔记-常用命令 常用sql: SHOW VARIABLES LIKE 'character%';查看字符集SHOW VARIABLES LIKE 'collation_%';show e ...
- 在Eclipse中编译maven项目出的问题
在Eclipse中编译Maven项目,运行 jetty:run 指令的时候会出错,在 JRE选项卡中加入: -Dorg.mortbay.util.URI.charset=GBK-Xms512m -Xm ...
- 利用谷歌 kaptcha 进行验证码生成
package main.com.smart.controller; import com.google.code.kaptcha.Producer; import main.com.smart.ut ...