c#List结合IEqualityComparer求交集
Posted 雨文100
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#List结合IEqualityComparer求交集相关的知识,希望对你有一定的参考价值。
List元素类:
public class MultiPointSearchingRet { public int ID { get; set; } public string PlateNumber { get; set; } public int VehicleBrand1 { get; set; } public string BrandName { get; set; } }
实现了IEqualityComparer的比较类:
public class MultiPointSearchingRetComparer : IEqualityComparer<MultiPointSearchingRet> { public bool Equals(MultiPointSearchingRet x, MultiPointSearchingRet y) { return x.PlateNumber == y.PlateNumber && x.VehicleBrand1 == y.VehicleBrand1; } public int GetHashCode(MultiPointSearchingRet obj) { return obj.ID.GetHashCode(); } }
调用:
List<MultiPointSearchingRet>[] tmpLst = new List<MultiPointSearchingRet>[10]; List<MultiPointSearchingRet> retLst = new List<MultiPointSearchingRet>(); //略 for (int i = 0; i < length; i++) { if (i == 0) retLst = tmpLst[i]; else retLst = retLst.Intersect(tmpLst[i], new MultiPointSearchingRetComparer()).ToList(); }
以上是关于c#List结合IEqualityComparer求交集的主要内容,如果未能解决你的问题,请参考以下文章
IEqualityComparer<TSource> 比较规则
C# Distinct on IEnumerable<T> 与自定义 IEqualityComparer