2022-06-04 13:31:48 +02:00

24 lines
750 B
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace DHT.Utils.Collections {
public static class LinqExtensions {
public static IEnumerable<TItem> DistinctByKeyStable<TItem, TKey>(this IEnumerable<TItem> collection, Func<TItem, TKey> getKeyFromItem) where TKey : IEquatable<TKey> {
HashSet<TKey>? seenKeys = null;
foreach (var item in collection) {
seenKeys ??= new HashSet<TKey>();
if (seenKeys.Add(getKeyFromItem(item))) {
yield return item;
}
}
}
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<(TKey, TValue)> collection) where TKey : notnull {
return collection.ToDictionary(static item => item.Item1, static item => item.Item2);
}
}
}