Left outer join in LINQ query
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
LEFT OUTER JOIN in LINQ
号
如何使用左外部联接进行LINQ查询?
可以对左外部联接使用Enumerable.DefaultifEmpty方法。
您可以看到:如何:执行左外部连接(C编程指南)-msdn
请考虑以下来自msdn的示例,
1 2 3 4 5 6 7 8 9 10 11 | class Person { public string FirstName { get; set; } public string LastName { get; set; } } class Pet { public string Name { get; set; } public Person Owner { get; set; } } |
左外部联接查询可以是:
1 2 3 4 | var query = from person in people join pet in pets on person equals pet.Owner into gj from subpet in gj.DefaultIfEmpty() select new { person.FirstName, PetName = (subpet == null ? String.Empty : subpet.Name) }; |
号
1 2 3 4 | var query = (from t1 in Context.Table1 join t2temp in Context.Table2 on t1.Id equals t2.Id into tempJoin from t2 in tempJoin.DefaultIfEmpty() select ...); |