mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 03:01:24 +00:00
32 lines
1.0 KiB
C#
32 lines
1.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace MewtocolNet.Helpers {
|
|
|
|
internal static class LinqHelpers {
|
|
|
|
internal static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) {
|
|
return DistinctBy(source, keySelector, null);
|
|
}
|
|
|
|
internal static IEnumerable<TSource> DistinctBy<TSource, TKey>
|
|
(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) {
|
|
|
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
|
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
|
|
|
|
return _(); IEnumerable<TSource> _() {
|
|
var knownKeys = new HashSet<TKey>(comparer);
|
|
foreach (var element in source) {
|
|
if (knownKeys.Add(keySelector(element)))
|
|
yield return element;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|