1
0
mirror of https://github.com/chylex/Discord-History-Tracker.git synced 2024-10-17 09:42:44 +02: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;
}
}
}
}