ToCollection.cs 7.3 KB

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