//原数据
List<T_Model> tmpListAll=new List<T_Model>();
//去除集合中重复数据后
List<T_Model> tmpList = tmpListAll.Distinct(new AutoFilterParmsCompare()).ToList();
/// <summary>
/// 比较器
/// </summary>
public class AutoFilterParmsCompare : EqualityComparer<T_Model>
{
public override bool Equals(T_Model x,T_Model y)
{
return x.InvoiceNo == y.InvoiceNo && x.InvoiceDate == y.InvoiceDate;
}
public override int GetHashCode(T_Model obj)
{
return obj.InvoiceNo.GetHashCode();
}
}
public class T_Model{
public string InvoiceNo{get;set;}
public string InvoiceDate {get;set;}
}
本文介绍了一种使用自定义比较器去除C#中泛型集合重复元素的方法,通过实例展示了如何根据特定属性(如发票号和发票日期)进行比较,从而实现集合的去重。
1420

被折叠的 条评论
为什么被折叠?



