1.对象列表的排重和求差。
定义一个test1类:
public class test1
{
public string aa { get; set; }
public string cc { get; set; }
}
实现比较方法:
public class Test1ListEquality : IEqualityComparer<test1>
{
public bool Equals(test1 x, test1 y)
{
return x.aa == y.aa && x.cc == y.cc;
}
public int GetHashCode(test1 obj)
{
return (obj == null) ? 0 : obj.ToString().GetHashCode();
}
}
A.用此方法可以求出差值,但性能很慢
List<test1> lstT = new List<test1>();
List<test1> lstT2 = new List<test1>();
for (int i = 1; i <= 100000; i++)
{
test1 tt = new test1();
tt.cc = i.ToString();
tt.aa = i.ToString();
lstT.Add(tt);
}
for (int i = 1; i <= 10000; i++)
{
test1 tt = new test1();
tt.cc = i.ToString();
tt.aa = i.ToString();
lstT2.Add(tt);
}
Stopwatch sw = new Stopwatch();
sw.Start();
var yy = lstT.Except(lstT2, new Test1ListEquality()).ToList();
//var yy = lstT.Select(p => new { p.cc, p.aa }).Except(lstT2.Select(p => new { p.cc, p.aa })).ToList();
sw.Stop();
MessageBox.Show(sw.ElapsedMilliseconds.ToString());
B.Select去重,但返回值类型变了,性能提高了N倍,同样能求差。
var yy = lstT.Select(p => new { p.cc, p.aa }).Except(lstT2.Select(p => new { p.cc, p.aa })).ToList();
2.去重
A.Test1ListEquality()方法
List<test1> lstT = new List<test1>();
for (int i = 1; i <= 100000; i++)
{
test1 tt = new test1();
tt.cc = (i%4).ToString();
tt.aa = (i%4).ToString();
lstT.Add(tt);
}
Stopwatch sw = new Stopwatch();
sw.Start();
var yy = lstT.Distinct(new Test1ListEquality()).ToList();
//var yy = lstT.Select(p=> new { p.cc, p.aa }).Distinct().ToList();
//var yy = lstT.Where((x, i) => lstT.FindIndex(z => z.aa == x.aa && z.cc == x.cc) == i).ToList();
sw.Stop();
MessageBox.Show(sw.ElapsedMilliseconds.ToString());
B.Select去重,返回类型变了
var yy = lstT.Select(p=> new { p.cc, p.aa }).Distinct().ToList();
C.使用lamdba表达式去重,返回值保持不变
var yy = lstT.Where((x, i) => lstT.FindIndex(z => z.aa == x.aa && z.cc == x.cc) == i).ToList();
通用去重方法:
public class Compare<T, C> : IEqualityComparer<T>
{
private Func<T, C> _getField;
public Compare(Func<T, C> getfield)
{
this._getField = getfield;
}
public bool Equals(T x, T y)
{
return EqualityComparer<C>.Default.Equals(_getField(x), _getField(y));
}
public int GetHashCode(T obj)
{
return EqualityComparer<C>.Default.GetHashCode(this._getField(obj));
}
}
public static class CommonHelper
{
/// <summary>
/// 自定义Distinct扩展方法
/// </summary>
/// <typeparam name="T">要去重的对象类</typeparam>
/// <typeparam name="C">自定义去重的字段类型</typeparam>
/// <param name="source">要去重的对象</param>
/// <param name="getfield">获取自定义去重字段的委托</param>
/// <returns></returns>
public static IEnumerable<T> MyDistinct<T, C>(this IEnumerable<T> source, Func<T, C> getfield)
{
return source.Distinct(new Compare<T, C>(getfield));
}
}