转载: 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 说明 ...
随机推荐
- 使程序能够引入.json文件, 为网站添加 MIME 映射
确认启用了 MIME 映射,或使用命令行工具 appcmd.exe 为网站添加 MIME 映射. 若要设置 MIME 类型,请在 IIS Express 安装目录中运行以下命令: appcmd set ...
- Memcached 深度分析
Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库 负载,提升性能.关于这个东西,相信很多人都用过,本文意在通过 ...
- char * 和 void *
POSIX.1 将 read函数的原型做了修改,经典的定义为 int read(int filedes, char *buf, unsigned nbytes); 修改为 ssize_t read(i ...
- linux 第二天
文件权限和用户组 groupadd 组名 groupadd policeman 查看组 vi /etc/group cat /etc/group 创建用户,并同时指定将该用户分配到哪个组 userad ...
- Linux升级C基本运行库CLIBC
在你准备升级GLIBC库之前,你要好好思考一下, 你真的要升级GLIBC么? 你知道你自己在做什么么? glibc是gnu发布的libc库,即c运行库.glibc是linux系统中最底层的api,几乎 ...
- rtsp转发服务器设计
做一个设备实时监控.需求是这样的,一个用户有多个设备(android系统,支持摄像头),设备分布在家中或者其它地方:用户可以远程通过终端(手机.pc.ipad,etc...)管理操纵这些设备(包括实时 ...
- ajaxfileupload
} } setTimeout( }, s. ...
- 学习OpenSeadragon之一(一个显示多层图片的开源JS库)
OpenSeadragon是一个可以显示多层图片(可放大缩小)的Web库,基于JavaScript,支持桌面和手机. 由于我项目需要,却没有找到任何中文教程,因此在官网上一边学习,一边总结于此. 官网 ...
- 2014年企业改善IT风险管理的5个办法
进入新的一年,企业面对数据泄密事故.日益复杂的攻击和对其控制的持续监管,现在是时候重新审视其风险管理战略了.虽然每个企业都是独特的,风险管理专家认为有些风险管理办法值得企业关注.下面我们列出了5个风险 ...
- wdcp/wdlinux 常用工具及命令集
强制修改Mysql的root密码,在忘记mysql密码时有用 sh /www/Wdlinux/tools/mysql_root_chg.sh ftp配置文件检查,在FTP不能连接或登录时有用sh /w ...