我正在尝试在三个表上执行左外连接。我知道有一种方法可以在一个变量中完成它但我无法弄明白。相反,我已经执行了两个单独的左外连接,然后在那些上做了一个左外连接。这是我的代码:
var outerJoin1 =
(from h in resultHours
join u in results on new {h.PhysicalUnitId, h.MonthNum} equals new {u.PhysicalUnitId, u.MonthNum} into outer
from grouping in outer.DefaultIfEmpty()
select new {timeKey = h, Key = grouping});
var outerJoin2 =
(from h in resultHours
join s in serviceHrsResults on new {h.PhysicalUnitId, h.MonthNum} equals new {s.PhysicalUnitId, s.MonthNum} into outer2
from grouping in outer2.DefaultIfEmpty()
select new {timeKey = h, Key = grouping});
var outerJoin =
(from a in outerJoin1
join b in outerJoin2 on new {a.timeKey.PlantId, a.timeKey.MonthNum} equals new {b.timeKey.PlantId, b.timeKey.MonthNum} into outer
from grouping in outer.DefaultIfEmpty()
select new{timeKey = a, Key = grouping}).Distinct();
我已经尝试将上述内容放在一个变量中我无法使其工作。这是我尝试过的:
var outerjoin =
from h in resultHours
join u in results on new {h.PhysicalUnitId, h.MonthNum} equals new {u.PhysicalUnitId, u.MonthNum} into outer
from grouping in outer.DefaultIfEmpty()
from hr in resultHours
join s in serviceHrsResults on new {hr.PhysicalUnitId, hr.MonthNum} equals new {s.PhysicalUnitId, s.MonthNum} into outer2
from grouping in outer2.DefaultIfEmpty()
select new {timeKey = h && timeKey = hr, Key = grouping};
这个问题是两个分组冲突。我很确定我只需要在select之前进行单个分组,但无法弄清楚如何使用“分组”并包含outer.DefaultIfEmpty()和outer2.DefaultIfEmpty()。
如果有人能够启发我,我将不胜感激。
答案 0 :(得分:1)
您正在使用重复的范围变量ID - 您有2x grouping
- 在结果集中您想要哪个分组?
- 这条生产线有望达到什么目标?
timeKey = h && timeKey = hr
- 您希望如何在投影中合并这些内容?
我会尝试下面的查询,但我已经更改了select
部分,因为您的合并版本对我没有意义。
让我知道,如果没有数据对你有用,那么测试它并不容易,所以我不能确定新版本是否合适。
var outerjoin =
from h in resultHours
join u in results on new {h.PhysicalUnitId, h.MonthNum} equals new {u.PhysicalUnitId, u.MonthNum} into outer
from grouping1 in outer.DefaultIfEmpty()
//from hr in resultHours
join s in serviceHrsResults on new {grouping1.PhysicalUnitId, grouping1.MonthNum} equals new {s.PhysicalUnitId, s.MonthNum} into outer2
from grouping2 in outer2.DefaultIfEmpty()
select new {timeKey = h, Key1 = grouping1, Key2 = grouping2};