转载: 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.JobName From TimeSheetLines TL Left 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.IndirectName From TimeSheetLines TL Left Join Jobs J on TL.JobNo=J.JobNo Left 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 说明 ...
随机推荐
- Because the people who are crazy enough to think they can change the world, are the ones who do.
Here's to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square h ...
- svg转换工具
package com.rubekid.springmvc.utils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOu ...
- junit测试用例加载spring配置文件
junit加载pom引用项目的xml配置文件,如果定义了<beans profile="dev">,必须在测试用例类上面加上标记 @ActiveProfiles(&qu ...
- Android 软件盘 动态设置 layout
总体来说分为三种方法: 在编辑框输入内容时会弹出软键盘,而手机屏幕区域有限往往会遮住输入界面,我们先看一下问题效果图: 输入用户名和密码时,系统会弹出键盘,造成系统键盘会挡住文本框的问题,如图所示: ...
- CAGradientLayer实现色差动画
效果图: 代码部分: RPGradientAnimationView.h #import <UIKit/UIKit.h> typedef enum : NSUInteger { RPGra ...
- PowerShell: 如何解决File **.ps1 cannot be loaded because the execution of scripts is disabled on this sy
PowerShell 默认不允许执行*.ps1脚本文件.运行ps1文件会得到下面的错误: File C:\Temp\Test.ps1 cannot be loaded because the exec ...
- phpcms v9二次开发之模型类的应用(1)
在<phpcms二次开发之模型类model.class.php>中讲到了模型类的建立方法,接下来我讲一下模型类的应用. 前段时间我基于phpcms v9开发了一个足球网.足球网是 ...
- 在线支付接口之PHP支付宝接口开发简单介绍
php100:92:在线支付接口之PHP支付宝接口开发 支付接口一般是第三方提供的代收款.付款的平台,可以通过支付接口帮助企业或个人利用一切可以使用的支付方式.常见支付平台:支付宝.快钱.云网支付.财 ...
- 统计建模与R软件习题二答案
# 习题2 # 2.1 x=c(1,2,3) y=c(4,5,6) e=c(rep(1,3)) z=2*x+y+e;z x%*%y # 若x,y如答案那样定义为矩阵,则不能用%*%,因为,维数不对应, ...
- mysql巡检脚本
#!/usr/bin/env python3.5 import psutil import mysql.connector import argparse import json import dat ...