标签:重复 div summary foreach key 转发 rgb word har
/// <summary> /// Distinct去除重复项(可自己指定) /// </summary> /// <typeparam name="TSource"></typeparam> /// <typeparam name="TKey"></typeparam> /// <param name="source"></param> /// <param name="keySelector"></param> /// <returns></returns> 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; } }
使用方法如下(针对ID,和Name进行Distinct)
var query = people.DistinctBy(p => new { p.Id, p.Name });
若仅仅针对ID进行distinct:
var query = people.DistinctBy(p => p.Id);
【转发】Distinct去除重复项(可自己指定字符进行筛选)
标签:重复 div summary foreach key 转发 rgb word har
原文地址:https://www.cnblogs.com/firstcsharp/p/14183613.html