[英]What is the best way to compare two list of complex object in c#
Hello friends I want to create a method that get two list of point (object that I'm created) and return 3 list that include: 您好朋友,我想创建一个方法,该方法获取两个点列表(我创建的对象)并返回3个列表,其中包括:
Points that exist in the first and the second list. 第一个和第二个列表中存在的点。
Points that exist in the first list and not exist in the second list. 第一个列表中存在而第二个列表中不存在的点。
Points that not exist in the first and exist in the second list. 在第一个列表中不存在且在第二个列表中存在的点。
What is the best wat to do that? 什么是最好的做法?
Point.cs: Point.cs:
Public class Point {public int X; public int Y}
First you need to create a IEqualityComparer<Point>
. 首先,您需要创建IEqualityComparer<Point>
。 Then use Intersect and Except Linq methods to achieve what you want. 然后使用“ 相交”和“ 除 Linq”方法来实现您想要的。
where points1 and points2 are your list of points. 其中points1和points2是您的点列表。
To implement IEqualityComparer, refer this question as a start. 要实现IEqualityComparer, 请将此问题作为开始。
You can do multiple things. 您可以做很多事情。
First use System.Drawing.Point
struct instead of your own class and you will be able to the comparison as well. 首先使用System.Drawing.Point
结构而不是您自己的类,您也将能够进行比较。
Second, If you have to create your own class then override Equals
and GetHashCode
in your class. 其次,如果必须创建自己的类,则在类中重写Equals
和GetHashCode
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.