ToCollection.cs 7.6 KB

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