Quickest way to find the complement of two collections in C#

前端 未结 3 2022
失恋的感觉
失恋的感觉 2021-01-01 12:02

I have two collections of type ICollection called c1 and c2. I\'d like to find the set of items that are in c2

3条回答
  •  别那么骄傲
    2021-01-01 12:46

    If using C# 3.0 + Linq:

    var complement = from i2 in c2
                     where c1.FirstOrDefault(i1 => i2.Id == i1.Id) == null
                     select i2;
    

    Loop through complement to get the items.

提交回复
热议问题