First.cs 4.8 KB

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