自定义比较器

 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 }

 

posted @ 2017-01-20 10:17  小小的菜鸟程序员  阅读(221)  评论(0编辑  收藏  举报