Using Linq Except not Working as I Thought

后端 未结 5 1415
梦如初夏
梦如初夏 2020-11-28 10:26

List1 contains items { A, B } and List2 contains items { A, B, C }.

What I need is to be returned { C }

5条回答
  •  青春惊慌失措
    2020-11-28 11:14

    You simply confused the order of arguments. I can see where this confusion arose, because the official documentation isn't as helpful as it could be:

    Produces the set difference of two sequences by using the default equality comparer to compare values.

    Unless you're versed in set theory, it may not be clear what a set difference actually is—it's not simply what's different between the sets. In reality, Except returns the list of elements in the first set that are not in the second set.

    Try this:

    var except = List2.Except(List1); // { C }
    

提交回复
热议问题