星空网 > 软件开发 > ASP.net

List集合去重的一种方法

前一段时间们需要对一个List<Model>集合去重,情况是该集合中会出现多个Name属性值相同的,但是其他属性值不同的数据。

在这种情况下,需求要只保留其中一个就好。

我觉得遍历和HashSet都不是我想要的,便采用了一下方式

定义Compare类,继承IEqualityComparer接口

public class ComparerHelper : IEqualityComparer<Model>  {    public bool Equals(Model x, Model y)    {      return x.Name== y.Name;    }    public int GetHashCode(Model obj)    {      return obj.Title.GetHashCode();    }  }

调用如下:

modelList = modelList.Distinct(new ComparerHelper()).ToList();

 




原标题:List集合去重的一种方法

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

IPQC 制程控制 :https://www.goluckyvip.com/tag/21590.html
iProspect:https://www.goluckyvip.com/tag/21592.html
Ipsy:https://www.goluckyvip.com/tag/21593.html
IPS智慧包裹:https://www.goluckyvip.com/tag/21594.html
IP地址:https://www.goluckyvip.com/tag/21595.html
iReviewHome:https://www.goluckyvip.com/tag/21598.html
instagram竞品“TikTok Notes”在加拿大和澳大利亚上线:https://www.goluckyvip.com/news/220211.html
青岛黄岛景点门票一览 - 青岛黄岛旅游景点介绍免费:https://www.vstour.cn/a/410226.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流