我不知道这是否涵盖所有情况,从逻辑上讲似乎是正确的。这个想法是采用左外部联接和右外部联接,然后取结果的并集。
var firstNames = new[]{ new { ID = 1, Name = "John" }, new { ID = 2, Name = "Sue" },};var lastNames = new[]{ new { ID = 1, Name = "Doe" }, new { ID = 3, Name = "Smith" },};var leftOuterJoin = from first in firstNames join last in lastNames on first.ID equals last.ID into temp from last in temp.DefaultIfEmpty() select new { first.ID, FirstName = first.Name, LastName = last?.Name, };var rightOuterJoin = from last in lastNames join first in firstNames on last.ID equals first.ID into temp from first in temp.DefaultIfEmpty() select new { last.ID, FirstName = first?.Name, LastName = last.Name, };var fullOuterJoin = leftOuterJoin.Union(rightOuterJoin);
由于它是在LINQ to Objects中编写的,因此按其编写的方式工作。如果是LINQ to
SQL或其他,则查询处理器可能不支持安全导航或其他 *** 作。您必须使用条件运算符有条件地获取值。
即
var leftOuterJoin = from first in firstNames join last in lastNames on first.ID equals last.ID into temp from last in temp.DefaultIfEmpty() select new { first.ID, FirstName = first.Name, LastName = last != null ? last.Name : default, };
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)