Geen omschrijving

LinqExtend.cs 629B

123456789101112131415161718192021222324
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace CallCenter.Utility.Linq
  7. {
  8. public static class LinqExtend
  9. {
  10. public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  11. {
  12. HashSet<TKey> seenKeys = new HashSet<TKey>();
  13. foreach (TSource element in source)
  14. {
  15. if (seenKeys.Add(keySelector(element)))
  16. {
  17. yield return element;
  18. }
  19. }
  20. }
  21. }
  22. }