LEFT OUTER JOIN in LINQ

后端 未结 22 3032
臣服心动
臣服心动 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:36

    Simple solution for the LEFT OUTER JOIN:

    var setA = context.SetA;
    var setB = context.SetB.Select(st=>st.Id).Distinct().ToList();
    var leftOuter  = setA.Where(stA=> !setB.Contains(stA.Id)); 
    

    notes:

    • To improve performance SetB could be converted to a Dictionary (if that is done then you have to change this: !setB.Contains(stA.Id)) or a HashSet
    • When there is more than one field involved this could be achieve using Set operations and a class that implement: IEqualityComparer

提交回复
热议问题