好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Distinct 扩展

Distinct 扩展

c# 扩展方法 奇思妙用 基础篇 八:Distinct 扩展

2011-08-01 20:57 by 鹤冲天, 1590 visits, 收藏 , 编辑

刚看了篇文章 《 Linq的Distinct太不给力了 》,文中给出了一个解决办法,略显复杂。

试想如果能写成下面的样子,是不是更简单优雅:

 1 
 2 
 var  p1 = products.Distinct(p => p.ID);
 var  p2 = products.Distinct(p => p.Name);

使用一个简单的 lambda 作为参数,也符合 Linq 一贯的风格。

可通过 扩展方法 实现:

Distinct 扩展方法

首先,创建一个通用比较的类,实现 IEqualityComparer<T> 接口:

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 using  System;
 using  System.Collections.Generic;
 using  System.Runtime.CompilerServices;
 using  System.Linq;

 public   class  CommonEqualityComparer<T, V> : IEqualityComparer<T>
{
     private  Func<T, V> keySelector;

     public  CommonEqualityComparer(Func<T, V> keySelector)
    {
         this .keySelector = keySelector;
    }

     public   bool  Equals(T x, T y)
    {
         return  EqualityComparer<V>.Default.Equals(keySelector(x), keySelector(y));
    }

     public   int  GetHashCode(T obj)
    {
         return  EqualityComparer<V>.Default.GetHashCode(keySelector(obj));
    }
}

第 17 行,用到了 EqualityComparer<T> 类 ,本文最后有 简要说明 。

借助上面这个类,Distinct 扩展方法 就很好写了:

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 public   static   class  DistinctExtensions
{
     public   static  IEnumerable<T> Distinct<T, V>( this  IEnumerable<T> source, Func<T, V> keySelector)
    {
         return  source.Distinct( new  CommonEqualityComparer<T, V>(keySelector));
    }
}

呵呵,简单吧!

Distinct 使用示例

根据 ID :

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 var  data1 =  new  Person[] {
     new  Person{ ID = 1, Name = " 鹤冲天 "},
     new  Person{ ID = 1, Name = " ldp "}
};
 var  ps1 = data1
    .Distinct(p => p.ID)
    .ToArray();

根据 Name:

 1 
 2 
 3 
 4 
 5 
 7 
 var  data2 =  new  Person[] {
     new  Person{ ID = 1, Name = " 鹤冲天 "},
     new  Person{ ID = 2, Name = " 鹤冲天 "}
};
 var  ps2 = data2
    .Distinct(p => p.Name)
    .ToArray();

看了回复后,我做了些改进,推荐使用下面的方式 :

改进

回复中有朋友提到“不区分大小写地排除重复的字符串”,也不难实现,只需要把上面的代码改进下就 OK:

CommonEqualityComparer<T, V> 类:

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 using  System;
 using  System.Collections.Generic;
 using  System.Runtime.CompilerServices;
 using  System.Linq;

 public   class  CommonEqualityComparer<T, V> : IEqualityComparer<T>
{
     private  Func<T, V> keySelector;
     private  IEqualityComparer<V> comparer;

     public  CommonEqualityComparer(Func<T, V> keySelector, IEqualityComparer<V> comparer)
    {
         this .keySelector = keySelector;
         this .comparer = comparer;
    }

     public  CommonEqualityComparer(Func<T, V> keySelector)
        :  this (keySelector, EqualityComparer<V>.Default)
    {  }

     public   bool  Equals(T x, T y)
    {
         return  comparer.Equals(keySelector(x), keySelector(y));
    }

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

Distinct 扩展方法 :

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 public   static   class  DistinctExtensions
{
     public   static  IEnumerable<T> Distinct<T, V>( this  IEnumerable<T> source, Func<T, V> keySelector)
    {
         return  source.Distinct( new  CommonEqualityComparer<T, V>(keySelector));
    }

     public   static  IEnumerable<T> Distinct<T, V>( this  IEnumerable<T> source, Func<T, V> keySelector, IEqualityComparer<V> comparer)
    {
         return  source.Distinct( new  CommonEqualityComparer<T, V>(keySelector, comparer));
    }
}

借助可选参数,这两个扩展方法也可以合成一个:

 1 
 2 
 3 
 4 
 5 
 public   static  IEnumerable<T> Distinct<T, V>( this  IEnumerable<T> source, Func<T, V> keySelector,
    IEqualityComparer<V> comparer = EqualityComparer<V>.Default)
{
     return  source.Distinct( new  CommonEqualityComparer<T, V>(keySelector, comparer));
}

(同样,CommonEqualityComparer<T, V>类的两个构造函数也可以合二为一)

使用示例:

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 var  data3 =  new  Person[] {
     new  Person{ ID = 1, Name = " LDP "},
     new  Person{ ID = 2, Name = " ldp "}
};
 var  ps3 = data3
    .Distinct(p => p.Name, StringComparer.CurrentCultureIgnoreCase)
    .ToArray();

EqualityComparer<T> 类 简要说明

EqualityComparer<T>为 IEqualityComparer<T> 泛型接口的实现提供基类,它在 .net 4 中有五个重要的子类,见下图:

这五个子类分别用不同类型数据的相等性比较,从类名我们可以略知一二。

这五个子类都是内部类(internal),不能直接访问,EqualityComparer<T> 类提供一个简单的属性 Default。EqualityComparer<T> 会根据传入的 T 的类型,加载不同的子类,并会予以缓存提高性能。


《 c#扩展方法奇思妙用 》系统文章从 2009 年 08 月开始写起,到现在一共有了 22 篇,欢迎阅读: 基础篇: 中文处理 、 string 常用扩展 、 byte 常用扩展 、 Random 扩展 、 Dictionary<TKey, TValue> 扩展 、 WhereIf 扩展 、 IsBetween 通用扩展 、 WhereIf 扩展 、 Distinct 扩展 高级篇: 改进 Scottgu 的 "In" 扩展 、 Aggregate扩展其改进 、 Enumerable.Cast<T>应用 、 对扩展进行分组管理 、 ToString(string format) 扩展 、 WinForm 控件选择器 、 树”通用遍历器 、 Type类扩展 变态篇: 由Fibonacci数列引出“委托扩展”及“递推递归委托” 、 封装 if/else、swith/case及while 、 switch/case 组扩展 、 string 的翻身革命 性能篇 : 扩展方法性能初测 MVC篇: 巧用扩展方法优先级,美化所有页面TextBoxFor文本框

-------------------

思想火花,照亮世界

分类: 扩展方法 , c#

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于Distinct 扩展的详细内容...

  阅读:39次