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;

    }

  }

}

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