c# list对象集合 去重
2021-01-28 18:13
阅读:662
标签:eth tor distinct ati name 集合 ring ram 重复
新建一个类,实现IEqualityComparer接口。注意GetHashCode方法的实现,只有HashCode相同才会去比较
public class Compare:IEqualityComparer{ public bool Equals(Student x,Student y) { return x.Id == y.Id;//可以自定义去重规则,此处将Id相同的就作为重复记录,不管学生的爱好是什么 } public int GetHashCode(Student obj) { return obj.Id.GetHashCode(); } }
调用:list.Distinct(new Compare()).ToList().ForEach(s => Console.WriteLine(s.ToString()));
列:
////// list对象去重 /// public class Compare : IEqualityComparer { /// /// /// /// /// /// public bool Equals(HistoryStatisticsDTO x, HistoryStatisticsDTO y) { if (x == null || y == null) return false; if (x.CaseNumId == x.CaseNumId) { return true; } else { return false; } } /// /// /// /// /// public int GetHashCode(HistoryStatisticsDTO obj) { if (obj == null) { return 0; } else { return obj.CaseNumId.GetHashCode(); } } }
调用: var list=historyStatistics.Distinct(new Compare()).ToList();
c# list对象集合 去重
标签:eth tor distinct ati name 集合 ring ram 重复
原文地址:https://www.cnblogs.com/zhangzhiping35/p/11881309.html
上一篇:ApiPost
评论
亲,登录后才可以留言!