ToCollection.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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("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. return source.Aggregate(new List<TSource>(), (list, x) =>
  24. {
  25. list.Add(x);
  26. return list;
  27. }, list => list.ToArray(), cancellationToken);
  28. }
  29. 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)
  30. {
  31. if (source == null)
  32. throw new ArgumentNullException("source");
  33. if (keySelector == null)
  34. throw new ArgumentNullException("keySelector");
  35. if (elementSelector == null)
  36. throw new ArgumentNullException("elementSelector");
  37. if (comparer == null)
  38. throw new ArgumentNullException("comparer");
  39. return ToDictionary(source, keySelector, elementSelector, comparer, CancellationToken.None);
  40. }
  41. public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  42. {
  43. if (source == null)
  44. throw new ArgumentNullException("source");
  45. if (keySelector == null)
  46. throw new ArgumentNullException("keySelector");
  47. if (elementSelector == null)
  48. throw new ArgumentNullException("elementSelector");
  49. return ToDictionary(source, keySelector, elementSelector, CancellationToken.None);
  50. }
  51. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  52. {
  53. if (source == null)
  54. throw new ArgumentNullException("source");
  55. if (keySelector == null)
  56. throw new ArgumentNullException("keySelector");
  57. if (comparer == null)
  58. throw new ArgumentNullException("comparer");
  59. return ToDictionary(source, keySelector, comparer, CancellationToken.None);
  60. }
  61. public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  62. {
  63. if (source == null)
  64. throw new ArgumentNullException("source");
  65. if (keySelector == null)
  66. throw new ArgumentNullException("keySelector");
  67. return ToDictionary(source, keySelector, CancellationToken.None);
  68. }
  69. 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)
  70. {
  71. if (source == null)
  72. throw new ArgumentNullException(nameof(source));
  73. if (keySelector == null)
  74. throw new ArgumentNullException(nameof(keySelector));
  75. if (elementSelector == null)
  76. throw new ArgumentNullException(nameof(elementSelector));
  77. if (comparer == null)
  78. throw new ArgumentNullException(nameof(comparer));
  79. return source.Aggregate(new Dictionary<TKey, TElement>(comparer), (d, x) =>
  80. {
  81. d.Add(keySelector(x), elementSelector(x));
  82. return d;
  83. }, cancellationToken);
  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("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. return source.Aggregate(new List<TSource>(), (list, x) =>
  124. {
  125. list.Add(x);
  126. return list;
  127. }, cancellationToken);
  128. }
  129. }
  130. }