自定义比较器
1 /// <summary> 2 /// 自定义比较器 ZhangQC 2016.11.29 3 /// </summary> 4 /// <typeparam name="T"></typeparam> 5 /// <typeparam name="TV"></typeparam> 6 public class CommonEqualityComparer<T, TV> : IEqualityComparer<T> 7 { 8 private readonly Func<T, TV> _keySelector; 9 10 public CommonEqualityComparer(Func<T, TV> keySelector) 11 { 12 this._keySelector = keySelector; 13 } 14 public bool Equals(T x, T y) 15 { 16 return EqualityComparer<TV>.Default.Equals(_keySelector(x), _keySelector(y)); 17 } 18 19 public int GetHashCode(T obj) 20 { 21 return EqualityComparer<TV>.Default.GetHashCode(_keySelector(obj)); 22 } 23 }