如何在Lambda LINQ表达式中进行LEFT JOIN

如何将此表达式设为LEFT JOIN

var query = order.Items.Join(productNonCriticalityList, i => i.ProductID, p => p.ProductID, (i, p) => i); 

这是使用lambda表达式编写它的更复杂的方法:

 order.Items .GroupJoin ( productNonCriticalityList, i => i.ProductID, p => p.ProductID, (i, g) => new { i = i, g = g } ) .SelectMany ( temp => temp.g.DefaultIfEmpty(), (temp, p) => new { i = temp.i, p = p } ) 

我建议切换到语法,你可以使用into关键字。

它与方法语法完全相同,并且更具可读性(IMO)。

 (from l1 in myFirstDataSet join l2 in mySecondDataSet on l1. equals l2. into leftJ from lj in leftJ.DefaultIfEmpty() where  select ).ToList();