当前位置:首页 > C#教程 > C#高级

c#List结合IEqualityComparer求交集

 

list元素类:

            public             class              multipointsearchingret
    {
                     public             int id { get; set; }
        publicstring platenumber { get; set; }
        publicint vehiclebrand1 { get; set; }
        publicstring brandname { get; set; }
      }

实现了iequalitycomparer的比较类:

            public             class multipointsearchingretcomparer : iequalitycomparer<multipointsearchingret>     {
        publicbool equals(multipointsearchingret x, multipointsearchingret y)
        {
            return x.platenumber == y.platenumber && x.vehiclebrand1 == y.vehiclebrand1;
        }

        publicint 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();
            }

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!