0

我正在尝试在三个表上进行左外连接。我知道有一种方法可以在一个变量中做到这一点,但我无法弄清楚。相反,我执行了两个单独的左外连接,然后对它们进行了左外连接。这是我的代码:

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};

这个问题是两个分组冲突。我很确定我只需要在选择之前进行一个分组,但无法弄清楚如何使用“分组”并包括outer.DefaultIfEmpty()和outer2.DefaultIfEmpty()。

如果有人能启发我,我将不胜感激。

4

1 回答 1

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};
于 2012-06-07T07:14:14.790 回答