LINQ Except()方法不起作用

Nug*_*ugs 5 c# linq linq-to-objects

我有2 IList<T>个相同类型的对象ItemsDTO.我想从另一个列表中排除一个列表.然而,这对我来说似乎没有用,我想知道为什么?

IList<ItemsDTO> related = itemsbl.GetRelatedItems();
IList<ItemsDTO> relating = itemsbl.GetRelatingItems().Except(related).ToList();
Run Code Online (Sandbox Code Playgroud)

我正在尝试relatedrelating列表中删除项目.

Mag*_*nus 11

由于class是引用类型,因此您的ItemsDTO类必须重写EqualsGetHashCode使其工作.

  • @gprasant http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overriden-in-c (2认同)
  • ``除了`在内部使用`HashSets`. (2认同)