记录:linq自定义去重DistinctBy

创建一个cs文件,放进去即可 

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;
                }
            }
        }

使用:

list.DistinctBy(t => new { t.ID, t.Name});
发布了16 篇原创文章 · 获赞 0 · 访问量 5681

猜你喜欢

转载自blog.csdn.net/u013608482/article/details/88397968