码迷,mamicode.com
首页 > Windows程序 > 详细

c#List结合IEqualityComparer求交集

时间:2016-03-11 11:54:58      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:

 

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求交集

标签:

原文地址:http://www.cnblogs.com/zhengwk/p/5264767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!