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