Skip to content

Instantly share code, notes, and snippets.

@wipiano
Created February 2, 2018 08:44
Show Gist options
  • Save wipiano/047a4c0eff58331cfcd36ab87c22bcfc to your computer and use it in GitHub Desktop.
Save wipiano/047a4c0eff58331cfcd36ab87c22bcfc to your computer and use it in GitHub Desktop.
LINQ の拡張メソッド群
public static class LinqExtensions
{
// KeySelector を指定して distinct
public static IEnumerable<T> Distinct<T, TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector)
{
IEnumerable<T> Enumerate()
{
var set = new HashSet<TKey>();
foreach (var elem in source)
{
if (set.Add(keySelector(elem)))
{
yield return elem;
}
}
}
return source != null ? Enumerate() : throw new ArgumentNullException(nameof(source));
}
// TODO: Intersect, ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment