简体   繁体   English

在C#中比较复杂对象的两个列表的最佳方法是什么

[英]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个列表,其中包括:

  1. Points that exist in the first and the second list. 第一个和第二个列表中存在的点。

  2. Points that exist in the first list and not exist in the second list. 第一个列表中存在而第二个列表中不存在的点。

  3. 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”方法来实现您想要的。

  1. var result = points1.Intersect(points2, yourComparer); var result = points1.Intersect(points2,yourComparer);
  2. var result = points1.Except(points2, yourComparer); var结果= points1.Except(points2,yourComparer);
  3. var result = points2.Except(points1, yourComparer); var结果= points2.Except(points1,yourComparer);

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. 其次,如果必须创建自己的类,则在类中重写EqualsGetHashCode

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

相关问题 比较 c# 中两个对象的最佳方法是什么 - What is the best way to compare two objects in c# 比较两个大型列表的最佳方法,C# - Best way to compare two large list, C# 在 c# 中将 100 万个对象列表与另一个 100 万个对象列表进行比较的最佳方法 - Best Way to compare 1 million List of object with another 1 million List of object in c# 比较两个复杂对象列表 - Compare two list of complex object 从MVC3应用程序序列化,返回和使用复杂的C#对象的最佳方法是什么? - What is the best way to serialize, return and consume complex C# object from an MVC3 app? 比较两个复杂对象的最佳方法 - Best way to compare two complex objects C#比较双精度值和一系列值并返回两个文本值的最佳方法是什么 - C# What is the best way to compare a double value to a range of values and return two text values 在 C# 中比较两个 DateTime 的相等性的最佳方法是什么……但只能达到一定的精度? - What's the best way to compare the equality of two DateTime's in C#… but only to a certain precision? C#最好的方式来比较一天中的两个时间 - C# best way to compare two time of the day 比较C#中忽略大小写的2个字符的最佳方法是什么? - What is the best way to compare 2 characters ignoring case in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM