现在的位置: 首页 > 综合 > 正文

C#过滤重复数据,使用泛型

2011年09月07日 ⁄ 综合 ⁄ 共 748字 ⁄ 字号 评论关闭
#region List<T> 过滤重复数据
public delegate bool EqualsComparer<T>(T x, T y);
/// <summary>
/// 过滤重复数据
/// </summary>
public class Comparint<T> : IEqualityComparer<T>
{
private EqualsComparer<T> ec;
public Comparint() { }
public Comparint(EqualsComparer<T> e)
{
this.ec = e;
}
#region IEqualityComparer<T> 成员

public bool Equals(T x, T y)
{
if (null != this.ec)
return this.ec(x, y);
else
return false;
}

public int GetHashCode(T obj)
{
return obj.ToString().GetHashCode();
}

#endregion
}
#endregion

使用示例

string[] strList = oldMailList.Split(',');
string strNewMailList = "";

List<string> emailList = new List<string>();
try
{
emailList.AddRange(strList);//增加到List中
List<string> emailList2 = emailList.Distinct(new Comparint<string>(
delegate(string x, string y)
{
if (null != x && null != y)
return x.GetHashCode() == y.GetHashCode();
return false;
}
)).ToList();
}

抱歉!评论已关闭.