Linq Except with custom IEqualityComparer

后端 未结 3 778
陌清茗
陌清茗 2020-12-08 14:15

I am trying to find the difference between two generic lists, as in the example below. Even though t1 and t2 contain the same properties, they are not the same object, so I

3条回答
  •  遥遥无期
    2020-12-08 14:47

    You could try something like:

    var differences = list2.Where(l2 => 
        !list1.Any(l1 => l1.Name == l2.Name && l1.Size == l2.Size));
    

    Or if you prefer:

    var differences = list2.Where(l2 => 
        list1.All(l1 => l1.Name != l2.Name || l1.Size != l2.Size));
    

提交回复
热议问题