Created
February 2, 2018 08:44
-
-
Save wipiano/047a4c0eff58331cfcd36ab87c22bcfc to your computer and use it in GitHub Desktop.
LINQ の拡張メソッド群
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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