你的位置:首页 > ASP.net教程

[ASP.net教程]LINQ扩展实现去重复


/// <summary>
/// 自定义去重复
/// </summary>
public static IEnumerable<TSource> IDistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{


var seenKeys = new HashSet<TKey>();
return source.Where(element => seenKeys.Add(keySelector(element)));


}