ToCollection.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static Task<TSource[]> ToArray<TSource>(this IAsyncEnumerable<TSource> source)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  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. throw new ArgumentNullException(nameof(source));
  23. var arrayProvider = source as IIListProvider<TSource>;
  24. if (arrayProvider != null)
  25. return arrayProvider.ToArrayAsync(cancellationToken);
  26. return source.Aggregate(new List<TSource>(), (list, x) =>
  27. {
  28. list.Add(x);
  29. return list;
  30. }, list => list.ToArray(), cancellationToken);
  31. }
  32. 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)
  33. {
  34. if (source == null)
  35. throw new ArgumentNullException(nameof(source));
  36. if (keySelector == null)
  37. throw new ArgumentNullException(nameof(keySelector));
  38. if (elementSelector == null)
  39. throw new ArgumentNullException(nameof(elementSelector));
  40. if (comparer == null)
  41. throw new ArgumentNullException(nameof(comparer));
  42. return ToDictionary(source, keySelector, elementSelector, comparer, CancellationToken.None);
  43. }
  44. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  45. {
  46. if (source == null)
  47. throw new ArgumentNullException(nameof(source));
  48. if (keySelector == null)
  49. throw new ArgumentNullException(nameof(keySelector));
  50. if (elementSelector == null)
  51. throw new ArgumentNullException(nameof(elementSelector));
  52. return ToDictionary(source, keySelector, elementSelector, CancellationToken.None);
  53. }
  54. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  55. {
  56. if (source == null)
  57. throw new ArgumentNullException(nameof(source));
  58. if (keySelector == null)
  59. throw new ArgumentNullException(nameof(keySelector));
  60. if (comparer == null)
  61. throw new ArgumentNullException(nameof(comparer));
  62. return ToDictionary(source, keySelector, comparer, CancellationToken.None);
  63. }
  64. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  65. {
  66. if (source == null)
  67. throw new ArgumentNullException(nameof(source));
  68. if (keySelector == null)
  69. throw new ArgumentNullException(nameof(keySelector));
  70. return ToDictionary(source, keySelector, CancellationToken.None);
  71. }
  72. 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)
  73. {
  74. if (source == null)
  75. throw new ArgumentNullException(nameof(source));
  76. if (keySelector == null)
  77. throw new ArgumentNullException(nameof(keySelector));
  78. if (elementSelector == null)
  79. throw new ArgumentNullException(nameof(elementSelector));
  80. if (comparer == null)
  81. throw new ArgumentNullException(nameof(comparer));
  82. return source.Aggregate(new Dictionary<TKey, TElement>(comparer), (d, x) =>
  83. {
  84. d.Add(keySelector(x), elementSelector(x));
  85. return d;
  86. }, cancellationToken);
  87. }
  88. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken)
  89. {
  90. if (source == null)
  91. throw new ArgumentNullException(nameof(source));
  92. if (keySelector == null)
  93. throw new ArgumentNullException(nameof(keySelector));
  94. if (elementSelector == null)
  95. throw new ArgumentNullException(nameof(elementSelector));
  96. return source.ToDictionary(keySelector, elementSelector, EqualityComparer<TKey>.Default, cancellationToken);
  97. }
  98. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  99. {
  100. if (source == null)
  101. throw new ArgumentNullException(nameof(source));
  102. if (keySelector == null)
  103. throw new ArgumentNullException(nameof(keySelector));
  104. if (comparer == null)
  105. throw new ArgumentNullException(nameof(comparer));
  106. return source.ToDictionary(keySelector, x => x, comparer, cancellationToken);
  107. }
  108. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
  109. {
  110. if (source == null)
  111. throw new ArgumentNullException(nameof(source));
  112. if (keySelector == null)
  113. throw new ArgumentNullException(nameof(keySelector));
  114. return source.ToDictionary(keySelector, x => x, EqualityComparer<TKey>.Default, cancellationToken);
  115. }
  116. public static Task<List<TSource>> ToList<TSource>(this IAsyncEnumerable<TSource> source)
  117. {
  118. if (source == null)
  119. throw new ArgumentNullException(nameof(source));
  120. return ToList(source, CancellationToken.None);
  121. }
  122. public static Task<List<TSource>> ToList<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  123. {
  124. if (source == null)
  125. throw new ArgumentNullException(nameof(source));
  126. var listProvider = source as IIListProvider<TSource>;
  127. if (listProvider != null)
  128. return listProvider.ToListAsync(cancellationToken);
  129. return source.Aggregate(new List<TSource>(), (list, x) =>
  130. {
  131. list.Add(x);
  132. return list;
  133. }, cancellationToken);
  134. }
  135. }
  136. }