List<object>进行Distinct()去重

简介: 原文:List进行Distinct()去重有时我们会对一个list集合里的数据进行去重,C#提供了一个Distinct()方法直接可以点得出来。如果list中的T是个自定义对象时直接对集合Distinct是达不到去重的效果。
原文: List<object>进行Distinct()去重

有时我们会对一个list<T>集合里的数据进行去重,C#提供了一个Distinct()方法直接可以点得出来。如果list<T>中的T是个自定义对象时直接对集合Distinct是达不到去重的效果。我们需要新定义一个去重的类并继承IEqualityComparer接口重写Equals和GetHashCode方法,如下Demo

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 
 5 namespace MyTestCode
 6 {
 7     /// <summary>
 8     /// Description of DistinctDemo.
 9     /// </summary>
10     public class DistinctDemo
11     {
12         private static List<Student> list;
13         public DistinctDemo()
14         {
15         }
16         
17         public static void init()
18         {
19             list = new List<Student>{
20                 new Student{
21                     Id=1,
22                     Name="xiaoming",
23                     Age=22
24                 },
25                 new Student{
26                     Id=2,
27                     Name="xiaohong",
28                     Age=23
29                 },
30                 new Student{
31                     Id=2,
32                     Name="xiaohong",
33                     Age=23
34                 },
35             };
36         }
37         
38         public void Display()
39         {
40             list = list.Distinct(new ListDistinct()).ToList();
41             foreach (var element in list) {
42                 Console.WriteLine(element.Id +"/"+element.Name+"/"+element.Age);
43             }
44         }
45         
46     }
47     
48     public class Student
49     {
50         public int Id{get;set;}
51         public string Name{get;set;}
52         public int Age{get;set;}
53     }
54     
55     public class ListDistinct : IEqualityComparer<Student>
56     {
57         public bool Equals(Student s1,Student s2)
58         {
59             return (s1.Name == s2.Name);
60         }
61         
62         public int GetHashCode(Student s)
63         {
64             return s==null?0:s.ToString().GetHashCode();
65         }
66     }
67 }

 

目录
相关文章
|
开发框架 .NET
list 去重
list 去重
|
安全 Java 编译器
解决Java中的“Unchecked cast: java.lang.Object to java.util.List”问题
解决Java中的“Unchecked cast: java.lang.Object to java.util.List”问题
851 0
|
10月前
|
JavaScript
Vue报错 Invalid default value for prop “list“: Props with type Object/Array must use a factory
Vue报错 Invalid default value for prop “list“: Props with type Object/Array must use a factory
429 0
|
8月前
|
开发者 Python
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
211 0
|
9月前
|
Python
【已解决】AttributeError: ‘Index‘ object has no attribute ‘to_list‘
【已解决】AttributeError: ‘Index‘ object has no attribute ‘to_list‘
|
9月前
|
存储 NoSQL Redis
Redis第四弹,Redis实现list时候做出的优化ziplist(压缩链表,元素少的情况),可更好的节省空间list——(内部编码:quicklist)Object encoding
Redis第四弹,Redis实现list时候做出的优化ziplist(压缩链表,元素少的情况),可更好的节省空间list——(内部编码:quicklist)Object encoding
|
10月前
|
Java
Java使用List去重的四中方式
Java使用List去重的四中方式
61 6
|
10月前
|
SQL Java
【SpringBoot】List<实体类>如何去重,单、多属性去重
【SpringBoot】List<实体类>如何去重,单、多属性去重
610 0
|
10月前
|
JSON 数据格式 Python
TypeError the JSON object must be str, bytes or bytearray, not ‘list‘
TypeError the JSON object must be str, bytes or bytearray, not ‘list‘
235 1
|
10月前
|
Java 数据库连接 mybatis
mybatis返回结果为List<Map<String, Object>>的写法
mybatis返回结果为List<Map<String, Object>>的写法
928 1