Skip to content

Commit 0ea0535

Browse files
feat: add DuplicatesBy
1 parent 7eb09b4 commit 0ea0535

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/WouterVanRanst.Utils/Extensions/IEnumerableExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,14 @@ public static bool SequenceEqual<T>(this IEnumerable<T> first, IEnumerable<T> se
5454
return first.OrderBy(x => x).SequenceEqual(second.OrderBy(x => x));
5555
}
5656
}
57+
58+
public static IEnumerable<T> DuplicatesBy<T, TKey>(this IEnumerable<T> collection, Func<T, TKey> keySelector)
59+
{
60+
ArgumentNullException.ThrowIfNull(collection, nameof(collection));
61+
62+
return collection
63+
.GroupBy(keySelector)
64+
.Where(group => group.Count() > 1)
65+
.SelectMany(group => group);
66+
}
5767
}

0 commit comments

Comments
 (0)