LinqExtensions.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. namespace MediaBrowser.Model.Extensions
  4. {
  5. // MoreLINQ - Extensions to LINQ to Objects
  6. // Copyright (c) 2008 Jonathan Skeet. All rights reserved.
  7. //
  8. // Licensed under the Apache License, Version 2.0 (the "License");
  9. // you may not use this file except in compliance with the License.
  10. // You may obtain a copy of the License at
  11. //
  12. // http://www.apache.org/licenses/LICENSE-2.0
  13. //
  14. // Unless required by applicable law or agreed to in writing, software
  15. // distributed under the License is distributed on an "AS IS" BASIS,
  16. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. // See the License for the specific language governing permissions and
  18. // limitations under the License.
  19. public static class LinqExtensions
  20. {
  21. /// <summary>
  22. /// Returns all distinct elements of the given source, where "distinctness"
  23. /// is determined via a projection and the default equality comparer for the projected type.
  24. /// </summary>
  25. /// <remarks>
  26. /// This operator uses deferred execution and streams the results, although
  27. /// a set of already-seen keys is retained. If a key is seen multiple times,
  28. /// only the first element with that key is returned.
  29. /// </remarks>
  30. /// <typeparam name="TSource">Type of the source sequence</typeparam>
  31. /// <typeparam name="TKey">Type of the projected element</typeparam>
  32. /// <param name="source">Source sequence</param>
  33. /// <param name="keySelector">Projection for determining "distinctness"</param>
  34. /// <returns>A sequence consisting of distinct elements from the source sequence,
  35. /// comparing them by the specified key projection.</returns>
  36. public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
  37. Func<TSource, TKey> keySelector)
  38. {
  39. return source.DistinctBy(keySelector, null);
  40. }
  41. /// <summary>
  42. /// Returns all distinct elements of the given source, where "distinctness"
  43. /// is determined via a projection and the specified comparer for the projected type.
  44. /// </summary>
  45. /// <remarks>
  46. /// This operator uses deferred execution and streams the results, although
  47. /// a set of already-seen keys is retained. If a key is seen multiple times,
  48. /// only the first element with that key is returned.
  49. /// </remarks>
  50. /// <typeparam name="TSource">Type of the source sequence</typeparam>
  51. /// <typeparam name="TKey">Type of the projected element</typeparam>
  52. /// <param name="source">Source sequence</param>
  53. /// <param name="keySelector">Projection for determining "distinctness"</param>
  54. /// <param name="comparer">The equality comparer to use to determine whether or not keys are equal.
  55. /// If null, the default equality comparer for <c>TSource</c> is used.</param>
  56. /// <returns>A sequence consisting of distinct elements from the source sequence,
  57. /// comparing them by the specified key projection.</returns>
  58. public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
  59. Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  60. {
  61. if (source == null) throw new ArgumentNullException("source");
  62. if (keySelector == null) throw new ArgumentNullException("keySelector");
  63. return DistinctByImpl(source, keySelector, comparer);
  64. }
  65. private static IEnumerable<TSource> DistinctByImpl<TSource, TKey>(IEnumerable<TSource> source,
  66. Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  67. {
  68. var knownKeys = new HashSet<TKey>(comparer);
  69. foreach (var element in source)
  70. {
  71. if (knownKeys.Add(keySelector(element)))
  72. {
  73. yield return element;
  74. }
  75. }
  76. }
  77. }
  78. }