LinqExtensions.cs 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. public static TSource[] ToArray<TSource>(this IEnumerable<TSource> source, int count)
  42. {
  43. if (source == null) throw new ArgumentNullException("source");
  44. if (count < 0) throw new ArgumentOutOfRangeException("count");
  45. var array = new TSource[count];
  46. int i = 0;
  47. foreach (var item in source)
  48. {
  49. array[i++] = item;
  50. }
  51. return array;
  52. }
  53. /// <summary>
  54. /// Returns all distinct elements of the given source, where "distinctness"
  55. /// is determined via a projection and the specified comparer for the projected type.
  56. /// </summary>
  57. /// <remarks>
  58. /// This operator uses deferred execution and streams the results, although
  59. /// a set of already-seen keys is retained. If a key is seen multiple times,
  60. /// only the first element with that key is returned.
  61. /// </remarks>
  62. /// <typeparam name="TSource">Type of the source sequence</typeparam>
  63. /// <typeparam name="TKey">Type of the projected element</typeparam>
  64. /// <param name="source">Source sequence</param>
  65. /// <param name="keySelector">Projection for determining "distinctness"</param>
  66. /// <param name="comparer">The equality comparer to use to determine whether or not keys are equal.
  67. /// If null, the default equality comparer for <c>TSource</c> is used.</param>
  68. /// <returns>A sequence consisting of distinct elements from the source sequence,
  69. /// comparing them by the specified key projection.</returns>
  70. public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
  71. Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  72. {
  73. if (source == null) throw new ArgumentNullException("source");
  74. if (keySelector == null) throw new ArgumentNullException("keySelector");
  75. return DistinctByImpl(source, keySelector, comparer);
  76. }
  77. private static IEnumerable<TSource> DistinctByImpl<TSource, TKey>(IEnumerable<TSource> source,
  78. Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  79. {
  80. var knownKeys = new HashSet<TKey>(comparer);
  81. foreach (var element in source)
  82. {
  83. if (knownKeys.Add(keySelector(element)))
  84. {
  85. yield return element;
  86. }
  87. }
  88. }
  89. }
  90. }