【发布时间】:2017-05-14 16:01:05
【问题描述】:
我在LINQ - Full Outer Join找到了这段代码:
var leftOuterJoin = from first in firstNames
join last in lastNames
on first.ID equals last.ID
into temp
from last in temp.DefaultIfEmpty(new { first.ID, Name = default(string) })
select new
{
first.ID,
FirstName = first.Name,
LastName = last.Name,
};
var rightOuterJoin = from last in lastNames
join first in firstNames
on last.ID equals first.ID
into temp
from first in temp.DefaultIfEmpty(new { last.ID, Name = default(string) })
select new
{
last.ID,
FirstName = first.Name,
LastName = last.Name,
};
包含此代码的答案得到了很多支持,但我发现这里有问题。在左外连接中,它使用into 和from 与第二个实体,在右外连接中它也是如此。所以,这两种实现对我来说似乎都是一样的。你能告诉我左外连接和右外连接的区别吗?提前致谢。
编辑:
我认为右外连接应该是这样的:
var rightOuterJoin = from first in firstNames
join last in lastNames
on first.ID equals last.ID
into temp
from first in temp.DefaultIfEmpty(new { last.ID, Name = default(string) })
select new
{
last.ID,
FirstName = first.Name,
LastName = last.Name,
};
【问题讨论】:
-
在这个网站上也是这样:dotnetfunda.com/codes/show/1849/…我真的看不出有什么不同:(
-
我不明白这个问题。你把左边的东西变成右边的东西的方法是交换左右的东西。这就是这里所做的。为什么你认为这是错误的?
-
temp变量在左连接中是IEnumerable<Second>类型,但在右连接中是IEnumerable<First>类型,所以有区别。 -
@EricLippert 如果我将左连接更改为编辑中的代码,它会起作用吗?
-
@EricLippert 我不明白我们为什么要切换顺序?