付出与汲取

IEnumerable对象的Distinct方法重写

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;

    }

  }

}

posted on 2019-04-23 10:41  付出与汲取  阅读(487)  评论(0编辑  收藏  举报

导航