码迷,mamicode.com
首页 > 编程语言 > 详细

IEnumerable对象的Distinct方法重写

时间:2019-04-23 11:11:52      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:方法重写   hash   key   his   ati   方法   ble   ret   add   

public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)

{

  HashSet<TKey> hashSet = new HashSet<TKey>();

  foreach (TSource element in source)

  {

    if (hashSet.Add(keySelector(element)))

    {

      yield return element;

    }

  }

}

IEnumerable对象的Distinct方法重写

标签:方法重写   hash   key   his   ati   方法   ble   ret   add   

原文地址:https://www.cnblogs.com/chensong0524/p/10755018.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!