First.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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> First<TSource>(this IAsyncEnumerable<TSource> source)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. return First(source, CancellationToken.None);
  16. }
  17. public static Task<TSource> First<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  18. {
  19. if (source == null)
  20. throw new ArgumentNullException(nameof(source));
  21. if (predicate == null)
  22. throw new ArgumentNullException(nameof(predicate));
  23. return First(source, predicate, CancellationToken.None);
  24. }
  25. public static Task<TSource> First<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
  26. {
  27. if (source == null)
  28. throw new ArgumentNullException(nameof(source));
  29. if (predicate == null)
  30. throw new ArgumentNullException(nameof(predicate));
  31. return First(source, predicate, CancellationToken.None);
  32. }
  33. public static Task<TSource> First<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  34. {
  35. if (source == null)
  36. throw new ArgumentNullException(nameof(source));
  37. return First_(source, cancellationToken);
  38. }
  39. public static Task<TSource> First<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  40. {
  41. if (source == null)
  42. throw new ArgumentNullException(nameof(source));
  43. if (predicate == null)
  44. throw new ArgumentNullException(nameof(predicate));
  45. return source.Where(predicate)
  46. .First(cancellationToken);
  47. }
  48. public static Task<TSource> First<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  49. {
  50. if (source == null)
  51. throw new ArgumentNullException(nameof(source));
  52. if (predicate == null)
  53. throw new ArgumentNullException(nameof(predicate));
  54. return source.Where(predicate)
  55. .First(cancellationToken);
  56. }
  57. public static Task<TSource> FirstOrDefault<TSource>(this IAsyncEnumerable<TSource> source)
  58. {
  59. if (source == null)
  60. throw new ArgumentNullException(nameof(source));
  61. return FirstOrDefault(source, CancellationToken.None);
  62. }
  63. public static Task<TSource> FirstOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  64. {
  65. if (source == null)
  66. throw new ArgumentNullException(nameof(source));
  67. if (predicate == null)
  68. throw new ArgumentNullException(nameof(predicate));
  69. return FirstOrDefault(source, predicate, CancellationToken.None);
  70. }
  71. public static Task<TSource> FirstOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
  72. {
  73. if (source == null)
  74. throw new ArgumentNullException(nameof(source));
  75. if (predicate == null)
  76. throw new ArgumentNullException(nameof(predicate));
  77. return FirstOrDefault(source, predicate, CancellationToken.None);
  78. }
  79. public static Task<TSource> FirstOrDefault<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  80. {
  81. if (source == null)
  82. throw new ArgumentNullException(nameof(source));
  83. return FirstOrDefault_(source, cancellationToken);
  84. }
  85. public static Task<TSource> FirstOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  86. {
  87. if (source == null)
  88. throw new ArgumentNullException(nameof(source));
  89. if (predicate == null)
  90. throw new ArgumentNullException(nameof(predicate));
  91. return source.Where(predicate)
  92. .FirstOrDefault(cancellationToken);
  93. }
  94. public static Task<TSource> FirstOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  95. {
  96. if (source == null)
  97. throw new ArgumentNullException(nameof(source));
  98. if (predicate == null)
  99. throw new ArgumentNullException(nameof(predicate));
  100. return source.Where(predicate)
  101. .FirstOrDefault(cancellationToken);
  102. }
  103. private static async Task<TSource> First_<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  104. {
  105. var list = source as IList<TSource>;
  106. if (list?.Count > 0)
  107. {
  108. return list[0];
  109. }
  110. var e = source.GetAsyncEnumerator();
  111. try
  112. {
  113. if (await e.MoveNextAsync(cancellationToken)
  114. .ConfigureAwait(false))
  115. {
  116. return e.Current;
  117. }
  118. }
  119. finally
  120. {
  121. await e.DisposeAsync().ConfigureAwait(false);
  122. }
  123. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  124. }
  125. private static async Task<TSource> FirstOrDefault_<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  126. {
  127. var list = source as IList<TSource>;
  128. if (list?.Count > 0)
  129. {
  130. return list[0];
  131. }
  132. var e = source.GetAsyncEnumerator();
  133. try
  134. {
  135. if (await e.MoveNextAsync(cancellationToken)
  136. .ConfigureAwait(false))
  137. {
  138. return e.Current;
  139. }
  140. }
  141. finally
  142. {
  143. await e.DisposeAsync().ConfigureAwait(false);
  144. }
  145. return default(TSource);
  146. }
  147. }
  148. }