Skip to content

Instantly share code, notes, and snippets.

@sebnilsson
Last active December 18, 2015 14:19
Show Gist options
  • Save sebnilsson/5796261 to your computer and use it in GitHub Desktop.
Save sebnilsson/5796261 to your computer and use it in GitHub Desktop.
Get duplicates with LINQ
public static IEnumerable<TSource> Duplicates<TSource>(this IEnumerable<TSource> source)
{
return from grouping in source.GroupBy(x => x) where grouping.Count() > 1 from item in grouping select item;
}
public static IEnumerable<TSource> Duplicates<TSource>(
this IEnumerable<TSource> source, Func<TSource, object> keySelector)
{
return from grouping in source.GroupBy(keySelector)
where grouping.Count() > 1
from item in grouping
select item;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment