ToCollection.cs 7.6 KB

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