IEnumerable对象的Distinct方法重写
Posted chensong0524
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}
}
}
以上是关于IEnumerable对象的Distinct方法重写的主要内容,如果未能解决你的问题,请参考以下文章