1
0

ToCollection.cs 8.4 KB

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