根据其属性删除列表中的重复对象 [英] removing duplicate objects in a list based on its properties
问题描述
如何从列表中删除重复的对象?
例如:
列表(MyObject)列表= 新列表
myObject.SomeAttribute = x;
我想通过查看每个对象的属性来删除重复项.
可以使用一个有效的代码示例
请参见以下代码:
使用系统; 使用 System.Collections.Generic; 使用 System.Text; 使用 System.Linq; 命名空间 t1 { 类 MyClass { 公共 int ?属性{获取; 集;} 公共 int ID { get ; 集;} } 类 MyClassComparer:IEqualityComparer< MyClass> { 公共 布尔等于(MyClass x,MyClass y) { 如果(对象 .ReferenceEquals(x,y))返回 true ; 如果(对象.ReferenceEquals(x,空)|| 对象 .ReferenceEquals(y,空)) 返回 假; 返回 x.property == y.property; } 公共 int GetHashCode(MyClass myObject) { 如果(对象.ReferenceEquals(myObject,空))返回 0 ; int hashObjectProperty = myObject.property == 空? 0 :myObject.property.GetHashCode(); 返回 hashObjectProperty; } } class 程序 { 静态 无效 Main() { 列表< MyClass> list = 新 List< MyClass>(); Console.WriteLine(" ); foreach ( int 在 in 中可枚举.范围( 1 , 10 )) { list.Add(新 MyClass {ID = i,property =(i& 1 )})); Console.WriteLine(" ,i,i和 1 ); } Console.WriteLine(" ); foreach (在 列表中的MyClass myObject keyword>新 MyClassComparer()) ) { Console.WriteLine(" ,myObject.ID,myObject.财产); } Console.ReadKey(); } } }
如您所见,如果运行它,IEnumerble.Distinct
将在适当的比较器的帮助下为您完成这项工作,但是它将保留第一次出现的情况.因此,正如我在评论中提到的那样,您需要通过实现适当的 IEqualityComparer [< ^ ]-这也可以帮助您:用于Linq Distinct()的通用IEqualityComparer [ ^ ]
How do I remove duplicate objects from a list?
For example:
List(MyObject) list = new list
myObject.SomeAttribute = x;
I want to remove the duplicates by looking at each object''s attribute.
A working code example would be appreciated
See following code:
using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace t1 { class MyClass { public int? property {get; set;} public int ID {get; set;} } class MyClassComparer : IEqualityComparer<MyClass> { public bool Equals(MyClass x, MyClass y) { if (Object.ReferenceEquals(x, y)) return true; if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null)) return false; return x.property == y.property; } public int GetHashCode(MyClass myObject) { if (Object.ReferenceEquals(myObject, null)) return 0; int hashObjectProperty = myObject.property == null ? 0 : myObject.property.GetHashCode(); return hashObjectProperty; } } class Program { static void Main() { List<MyClass> list = new List<MyClass>(); Console.WriteLine("\nOriginal list:"); foreach(int i in Enumerable.Range(1,10)) { list.Add(new MyClass{ ID = i, property = (i & 1)}); Console.WriteLine("{0} => {1}", i, i & 1); } Console.WriteLine("\nDistinct by property:"); foreach(MyClass myObject in list.Distinct(new MyClassComparer())) { Console.WriteLine("{0} => {1}", myObject.ID, myObject.property); } Console.ReadKey(); } } }
As you will see if you run it,IEnumerble.Distinct
will do the job for you with the help of a proper comparer, but it will leave the first occurrences. Thus, as I mentioned in my comment, you need to define the comparison means by implementing a proper IEqualityComparer[^] - this one could also help you: A Generic IEqualityComparer for Linq Distinct()[^]
这篇关于根据其属性删除列表中的重复对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!