1
0
mirror of https://github.com/chylex/Discord-History-Tracker.git synced 2024-12-22 14:42:50 +01:00
Discord-History-Tracker/app/Utils/Collections/LinqExtensions.cs

19 lines
464 B
C#

using System;
using System.Collections.Generic;
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 ??= [];
if (seenKeys.Add(getKeyFromItem(item))) {
yield return item;
}
}
}
}