LEFT OUTER JOIN in LINQ

后端 未结 22 2506
臣服心动
臣服心动 2020-11-21 04:49

How to perform left outer join in C# LINQ to objects without using join-on-equals-into clauses? Is there any way to do that with where clause? Corr

22条回答
  •  醉酒成梦
    2020-11-21 05:20

    Here is a fairly easy to understand version using method syntax:

    IEnumerable outerLeft =
        lefts.SelectMany(l => 
            rights.Where(r => l.Key == r.Key)
                  .DefaultIfEmpty(new Item())
                  .Select(r => new JoinPair { LeftId = l.Id, RightId = r.Id }));
    

提交回复
热议问题