关于c#:LINQ中左外连接的等价物

Equivalent of Left outer join in LINQ

本问题已经有最佳答案,请猛点这里访问。

我有一个T-SQL查询

1
2
3
4
5
6
7
8
9
10
SELECT *
FROM BPM.tblEvaluationCatalogPlan
JOIN BPM.tblEvaluationCatalog ON BPM.tblEvaluationCatalogPlan.EvaluationCatalogId =
BPM.tblEvaluationCatalog.EvaluationCatalogId
JOIN BPM.tblMeasureProcess ON BPM.tblEvaluationCatalog.MeasureProcessId =
BPM.tblMeasureProcess.MeasureProcessId
JOIN dbo.tblMeasureCatalog ON BPM.tblMeasureProcess.MeasureCatalogId =
dbo.tblMeasureCatalog.MeasureCatalogId
LEFT OUTER JOIN dbo.tblMeasureUnit ON dbo.tblMeasureCatalog.MeasureUnitId =
dbo.tblMeasureUnit.MeasureUnitId

我已经为它写了一个LINQ查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from m in tblEvaluationCatalogPlan
join l in tblEvaluationCatalog.Where(y => y.PlanId == PlanId) on
m.EvaluationCatalogId equals l.EvaluationCatalogId
join k in tblMeasureProcess.Where(x => x.ProcessId == ProcessId) on
l.MeasureProcessId equals k.MeasureProcessId
join j in tblMeasureCatalog on k.MeasureCatalogId equals
j.MeasureCatalogId
join i in tblMeasureUnit on j.MeasureUnitId equals i.MeasureUnitId
select new EvaluationCatalogPlanEntity
   {
   EvaluationCatalogPlan = m,
   EvaluationCatalog = l,
   MeasureProcess = k,
   MeasureCatalog = j,
   MeasureUnit = i
   }

但它们的产量不一样。

LINQ中T-SQL查询的确切等价物是什么?


尝试http://msdn.microsoft.com/en-us/vstudio/ee908647.aspx leftouterjoin

1
2
3
4
5
 var leftFinal =
    from l in lefts
    join r in rights on l equals r.Left into lrs
    from lr in lrs.DefaultIfEmpty()
    select new { LeftId = l.Id, RightId = ((l.Key==r.Key) ? r.Id : 0 };

你应该把代码放在代码格式中,这样更容易阅读。

我已经能够通过小组加入来实现这个结果。请看这里:http://msdn.microsoft.com/en-us/library/bb397895(v=vs.120).aspx

应该可以得到你想要的东西。