ToDictionary.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. if (keySelector == null)
  16. throw new ArgumentNullException(nameof(keySelector));
  17. return ToDictionary(source, keySelector, CancellationToken.None);
  18. }
  19. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. if (keySelector == null)
  24. throw new ArgumentNullException(nameof(keySelector));
  25. return source.ToDictionary(keySelector, x => x, EqualityComparer<TKey>.Default, cancellationToken);
  26. }
  27. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. if (keySelector == null)
  32. throw new ArgumentNullException(nameof(keySelector));
  33. return ToDictionary(source, keySelector, CancellationToken.None);
  34. }
  35. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, CancellationToken cancellationToken)
  36. {
  37. if (source == null)
  38. throw new ArgumentNullException(nameof(source));
  39. if (keySelector == null)
  40. throw new ArgumentNullException(nameof(keySelector));
  41. return source.ToDictionary(keySelector, x => Task.FromResult(x), EqualityComparer<TKey>.Default, cancellationToken);
  42. }
  43. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  44. {
  45. if (source == null)
  46. throw new ArgumentNullException(nameof(source));
  47. if (keySelector == null)
  48. throw new ArgumentNullException(nameof(keySelector));
  49. if (comparer == null)
  50. throw new ArgumentNullException(nameof(comparer));
  51. return ToDictionary(source, keySelector, comparer, CancellationToken.None);
  52. }
  53. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  54. {
  55. if (source == null)
  56. throw new ArgumentNullException(nameof(source));
  57. if (keySelector == null)
  58. throw new ArgumentNullException(nameof(keySelector));
  59. if (comparer == null)
  60. throw new ArgumentNullException(nameof(comparer));
  61. return source.ToDictionary(keySelector, x => x, comparer, cancellationToken);
  62. }
  63. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer)
  64. {
  65. if (source == null)
  66. throw new ArgumentNullException(nameof(source));
  67. if (keySelector == null)
  68. throw new ArgumentNullException(nameof(keySelector));
  69. if (comparer == null)
  70. throw new ArgumentNullException(nameof(comparer));
  71. return ToDictionary(source, keySelector, comparer, CancellationToken.None);
  72. }
  73. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  74. {
  75. if (source == null)
  76. throw new ArgumentNullException(nameof(source));
  77. if (keySelector == null)
  78. throw new ArgumentNullException(nameof(keySelector));
  79. if (comparer == null)
  80. throw new ArgumentNullException(nameof(comparer));
  81. return source.ToDictionary(keySelector, x => Task.FromResult(x), comparer, cancellationToken);
  82. }
  83. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  84. {
  85. if (source == null)
  86. throw new ArgumentNullException(nameof(source));
  87. if (keySelector == null)
  88. throw new ArgumentNullException(nameof(keySelector));
  89. if (elementSelector == null)
  90. throw new ArgumentNullException(nameof(elementSelector));
  91. return ToDictionary(source, keySelector, elementSelector, CancellationToken.None);
  92. }
  93. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken)
  94. {
  95. if (source == null)
  96. throw new ArgumentNullException(nameof(source));
  97. if (keySelector == null)
  98. throw new ArgumentNullException(nameof(keySelector));
  99. if (elementSelector == null)
  100. throw new ArgumentNullException(nameof(elementSelector));
  101. return source.ToDictionary(keySelector, elementSelector, EqualityComparer<TKey>.Default, cancellationToken);
  102. }
  103. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, Func<TSource, Task<TElement>> elementSelector)
  104. {
  105. if (source == null)
  106. throw new ArgumentNullException(nameof(source));
  107. if (keySelector == null)
  108. throw new ArgumentNullException(nameof(keySelector));
  109. if (elementSelector == null)
  110. throw new ArgumentNullException(nameof(elementSelector));
  111. return ToDictionary(source, keySelector, elementSelector, CancellationToken.None);
  112. }
  113. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, Func<TSource, Task<TElement>> elementSelector, CancellationToken cancellationToken)
  114. {
  115. if (source == null)
  116. throw new ArgumentNullException(nameof(source));
  117. if (keySelector == null)
  118. throw new ArgumentNullException(nameof(keySelector));
  119. if (elementSelector == null)
  120. throw new ArgumentNullException(nameof(elementSelector));
  121. return source.ToDictionary(keySelector, elementSelector, EqualityComparer<TKey>.Default, cancellationToken);
  122. }
  123. 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)
  124. {
  125. if (source == null)
  126. throw new ArgumentNullException(nameof(source));
  127. if (keySelector == null)
  128. throw new ArgumentNullException(nameof(keySelector));
  129. if (elementSelector == null)
  130. throw new ArgumentNullException(nameof(elementSelector));
  131. if (comparer == null)
  132. throw new ArgumentNullException(nameof(comparer));
  133. return ToDictionary(source, keySelector, elementSelector, comparer, CancellationToken.None);
  134. }
  135. 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)
  136. {
  137. if (source == null)
  138. throw new ArgumentNullException(nameof(source));
  139. if (keySelector == null)
  140. throw new ArgumentNullException(nameof(keySelector));
  141. if (elementSelector == null)
  142. throw new ArgumentNullException(nameof(elementSelector));
  143. if (comparer == null)
  144. throw new ArgumentNullException(nameof(comparer));
  145. return source.Aggregate(
  146. new Dictionary<TKey, TElement>(comparer),
  147. (d, x) =>
  148. {
  149. d.Add(keySelector(x), elementSelector(x));
  150. return d;
  151. },
  152. cancellationToken
  153. );
  154. }
  155. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, Func<TSource, Task<TElement>> elementSelector, IEqualityComparer<TKey> comparer)
  156. {
  157. if (source == null)
  158. throw new ArgumentNullException(nameof(source));
  159. if (keySelector == null)
  160. throw new ArgumentNullException(nameof(keySelector));
  161. if (elementSelector == null)
  162. throw new ArgumentNullException(nameof(elementSelector));
  163. if (comparer == null)
  164. throw new ArgumentNullException(nameof(comparer));
  165. return ToDictionary(source, keySelector, elementSelector, comparer, CancellationToken.None);
  166. }
  167. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<TKey>> keySelector, Func<TSource, Task<TElement>> elementSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  168. {
  169. if (source == null)
  170. throw new ArgumentNullException(nameof(source));
  171. if (keySelector == null)
  172. throw new ArgumentNullException(nameof(keySelector));
  173. if (elementSelector == null)
  174. throw new ArgumentNullException(nameof(elementSelector));
  175. if (comparer == null)
  176. throw new ArgumentNullException(nameof(comparer));
  177. return source.Aggregate(
  178. new Dictionary<TKey, TElement>(comparer),
  179. async (d, x) =>
  180. {
  181. d.Add(await keySelector(x).ConfigureAwait(false), await elementSelector(x).ConfigureAwait(false));
  182. return d;
  183. },
  184. cancellationToken
  185. );
  186. }
  187. }
  188. }