Difference between two lists

前端 未结 12 1288
无人共我
无人共我 2020-11-22 13:19

I Have two generic list filled with CustomsObjects.

I need to retrieve the difference between those two lists(Items who are in the first without the items in the sec

12条回答
  •  耶瑟儿~
    2020-11-22 13:51

    var third = first.Except(second);
    

    (you can also call ToList() after Except(), if you don't like referencing lazy collections.)

    The Except() method compares the values using the default comparer, if the values being compared are of base data types, such as int, string, decimal etc.

    Otherwise the comparison will be made by object address, which is probably not what you want... In that case, make your custom objects implement IComparable (or implement a custom IEqualityComparer and pass it to the Except() method).

提交回复
热议问题