使用LINQ,从一个
List<int>
中,如何检索包含多个重复条目及其值的列表?Linq查询:
var query = from s2 in (from s in someList group s by new { s.Column1, s.Column2 } into sg select sg) where s2.Count() > 1 select s2;
所有的GroupBy
答案都是最简单的,但不会是最有效率的。它们对于内存性能特别差,因为构建大型内部集合具有分配成本。
一个不错的替代方案是HuBeZa的基于HashSet.Add
的方法。它的性能更好。
如果您不关心空值,我认为像这样的方法是最有效率的(无论是CPU还是内存):
public static IEnumerable<TProperty> Duplicates<TSource, TProperty>(
this IEnumerable<TSource> source,
Func<TSource, TProperty> duplicateSelector,
IEqualityComparer<TProperty> comparer = null)
{
comparer ??= EqualityComparer<TProperty>.Default;
Dictionary<TProperty, int> counts = new Dictionary<TProperty, int>(comparer);
foreach (var item in source)
{
TProperty property = duplicateSelector(item);
counts.TryGetValue(property, out int count);
switch (count)
{
case 0:
counts[property] = ++count;
break;
case 1:
counts[property] = ++count;
yield return property;
break;
}
}
}
通过键删除重复项
myTupleList = myTupleList.GroupBy(tuple => tuple.Item1).Select(group => group.First()).ToList();