Duplicates
Used in conjunction with a method like Ani's AssertCount method (I use one called CountAtLeast
), it becomes very easy to find elements in a sequence that appear more than once:
public static IEnumerable Duplicates(this IEnumerable source,
Func keySelector = null, IEqualityComparer comparer = null)
{
source.ThrowIfNull("source");
keySelector = keySelector ?? new Func(x => x);
comparer = comparer ?? EqualityComparer.Default;
return source.GroupBy(keySelector, comparer)
.Where(g => g.CountAtLeast(2))
.SelectMany(g => g);
}