LinqExtensions.cs 4.0 KB

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