C# 去重扩展类

    public static class IEnumerableEx
    {
        public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            HashSet<TKey> seenKeys = new HashSet<TKey>();
            foreach (TSource element in source)
            {
                if (seenKeys.Add(keySelector(element)))
                {
                    yield return element;
                }
            }
        }
    }

 

posted @ 2020-12-16 16:36  吃辣椒的小毛驴  阅读(89)  评论(0编辑  收藏  举报