ToDictionary.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. if (keySelector == null)
  16. throw new ArgumentNullException(nameof(keySelector));
  17. if (elementSelector == null)
  18. throw new ArgumentNullException(nameof(elementSelector));
  19. if (comparer == null)
  20. throw new ArgumentNullException(nameof(comparer));
  21. return ToDictionary(source, keySelector, elementSelector, comparer, CancellationToken.None);
  22. }
  23. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  24. {
  25. if (source == null)
  26. throw new ArgumentNullException(nameof(source));
  27. if (keySelector == null)
  28. throw new ArgumentNullException(nameof(keySelector));
  29. if (elementSelector == null)
  30. throw new ArgumentNullException(nameof(elementSelector));
  31. return ToDictionary(source, keySelector, elementSelector, CancellationToken.None);
  32. }
  33. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  34. {
  35. if (source == null)
  36. throw new ArgumentNullException(nameof(source));
  37. if (keySelector == null)
  38. throw new ArgumentNullException(nameof(keySelector));
  39. if (comparer == null)
  40. throw new ArgumentNullException(nameof(comparer));
  41. return ToDictionary(source, keySelector, comparer, CancellationToken.None);
  42. }
  43. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  44. {
  45. if (source == null)
  46. throw new ArgumentNullException(nameof(source));
  47. if (keySelector == null)
  48. throw new ArgumentNullException(nameof(keySelector));
  49. return ToDictionary(source, keySelector, CancellationToken.None);
  50. }
  51. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  52. {
  53. if (source == null)
  54. throw new ArgumentNullException(nameof(source));
  55. if (keySelector == null)
  56. throw new ArgumentNullException(nameof(keySelector));
  57. if (elementSelector == null)
  58. throw new ArgumentNullException(nameof(elementSelector));
  59. if (comparer == null)
  60. throw new ArgumentNullException(nameof(comparer));
  61. return source.Aggregate(
  62. new Dictionary<TKey, TElement>(comparer),
  63. (d, x) =>
  64. {
  65. d.Add(keySelector(x), elementSelector(x));
  66. return d;
  67. },
  68. cancellationToken
  69. );
  70. }
  71. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken)
  72. {
  73. if (source == null)
  74. throw new ArgumentNullException(nameof(source));
  75. if (keySelector == null)
  76. throw new ArgumentNullException(nameof(keySelector));
  77. if (elementSelector == null)
  78. throw new ArgumentNullException(nameof(elementSelector));
  79. return source.ToDictionary(keySelector, elementSelector, EqualityComparer<TKey>.Default, cancellationToken);
  80. }
  81. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  82. {
  83. if (source == null)
  84. throw new ArgumentNullException(nameof(source));
  85. if (keySelector == null)
  86. throw new ArgumentNullException(nameof(keySelector));
  87. if (comparer == null)
  88. throw new ArgumentNullException(nameof(comparer));
  89. return source.ToDictionary(keySelector, x => x, comparer, cancellationToken);
  90. }
  91. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
  92. {
  93. if (source == null)
  94. throw new ArgumentNullException(nameof(source));
  95. if (keySelector == null)
  96. throw new ArgumentNullException(nameof(keySelector));
  97. return source.ToDictionary(keySelector, x => x, EqualityComparer<TKey>.Default, cancellationToken);
  98. }
  99. }
  100. }