简体   繁体   English

如何获取通用列表中的比较项目

[英]How to get a Compare items in a Generic List

I have a custom class that implements that IComparable. 我有一个实现IComparable的自定义类。 This class is stored in a Generic List. 该类存储在通用列表中。 I now need to compare to lists to see which objects are in list A but not in list B. 我现在需要与列表进行比较,以查看哪些对象在列表A中但不在列表B中。

I thought the most simple way of doing this would be to iterate through list B and do A.contains(). 我认为最简单的方法是迭代列表B并执行A.contains()。

I do not know how to get it to use my CompareTo() (or another method that I can override so that I can say if it contains a certain object or not). 我不知道如何使用我的CompareTo()(或我可以覆盖的其他方法,以便我可以说它是否包含某个对象)。 I could be wrong but as I understand it the contains checks if the objects are actually the same (ie points to the same place in memory). 我可能是错的,但据我所知,包含检查对象实际上是否相同(即指向内存中的相同位置)。

Could anyone help me please? 有人可以帮帮我吗?

Why don't you just override the Equals method of your class to be consistent in meaning with CompareTo(other) == 0 ? 为什么不直接覆盖类的Equals方法以使其与CompareTo(other) == 0保持一致? This is the simplest way and also the most idiomatic since, as you've noticed, Contains compares equality rather than using CompareTo . 这是最简单的方式,也是最惯用的,因为正如您所注意到的, Contains比较了相等而不是使用CompareTo However, this check is done via Equals . 但是,此检查是通过Equals完成的。 It does not check whether the objects point to the same memory location. 检查对象是否指向同一个内存位置。

/EDIT: Additionally, if you're using .NET 3.5 you can use the Contains overload that accepts an IEqualityComparer argument. /编辑:此外,如果您使用的是.NET 3.5,则可以使用接受IEqualityComparer参数的Contains重载。 You can use this to provide a class that implements a custom equality relation for your class type. 您可以使用它来提供实现类类型的自定义相等关系的类。 However, I think the first method is more appropriate in your case. 但是,我认为第一种方法更适合您的情况。

Also if you are using framework 3.5 you can make a query like: 此外,如果您使用框架3.5,您可以进行如下查询:

list notInB = From item in listA where (listB.find(function(x) x.property = item.property) = nothing) select item list notInB = from listA中的项目(listB.find(function(x)x.property = item.property)= nothing)select item

This will return all the items of the listA that are not in listB. 这将返回listB中不在listB中的所有项目。

The linq function find returns the item that matches de condition that you write in the lambda function. linq函数find返回匹配在lambda函数中写入的de条件的项。 There you can add more conditions using and (&&)or or (||) 在那里你可以使用和(&&)或或(||)添加更多条件

If you use this, there is no need to implement IComparable. 如果使用它,则无需实现IComparable。

Thank you Konrad for such a quick and helpful reply. 感谢Konrad的快速回复。 Worked a treat! 做了一个享受! I used IComparable originally because I needed to sort them on certain criteria but overriding equals worked perfectly for .contains. 我最初使用IComparable是因为我需要按照某些标准对它们进行排序,但是重写equals对于.contains来说是完美的。

:) :)

it the contains checks if the objects are actually the same (ie points to the same place in memory) 它包含检查对象是否实际相同(即指向内存中的相同位置)

Note that object can be equal even if they do not point to the same place in memory. 请注意,即使对象未指向内存中的相同位置,该对象也可以相等。 It depends how EqualsTo method is implemented. 这取决于EqualsTo方法的实现方式。

I agree with Rudolph in that you first try to overload the EqualsTo(). 我同意鲁道夫的意见,你首先尝试重载EqualsTo()。

There is also IEquatable and if your object implements that interface, then it is automatically used by all the generic collections if its implemented. 还有IEquatable,如果你的对象实现了那个接口,那么如果它被实现,它将被所有泛型集合自动使用。 Though I never quite understood why it existed in the first place, since the docs say that if you implement IEquatable, then you should also override Equals to match. 虽然我从来没有完全理解为什么它首先存在,因为文档说如果你实现IEquatable,那么你也应该重写Equals来匹配。 Also remember if you are going to override Equals, that you need to also override GetHashCode as well. 还要记住,如果要覆盖Equals,还需要覆盖GetHashCode。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM