First.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  12. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.firstasync?view=net-9.0-pp#system-linq-asyncenumerable-firstasync-1(system-collections-generic-iasyncenumerable((-0))-system-threading-cancellationtoken)
  13. /// <summary>
  14. /// Returns the first element of an async-enumerable sequence.
  15. /// </summary>
  16. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  17. /// <param name="source">Source async-enumerable sequence.</param>
  18. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  19. /// <returns>ValueTask containing the first element in the async-enumerable sequence.</returns>
  20. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  21. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  22. public static ValueTask<TSource> FirstAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  23. {
  24. if (source == null)
  25. throw Error.ArgumentNull(nameof(source));
  26. return Core(source, cancellationToken);
  27. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  28. {
  29. var first = await TryGetFirst(source, cancellationToken).ConfigureAwait(false);
  30. return first.HasValue ? first.Value : throw Error.NoElements();
  31. }
  32. }
  33. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.firstasync?view=net-9.0-pp#system-linq-asyncenumerable-firstasync-1(system-collections-generic-iasyncenumerable((-0))-system-func((-0-system-boolean))-system-threading-cancellationtoken)
  34. /// <summary>
  35. /// Returns the first element of an async-enumerable sequence that satisfies the condition in the predicate.
  36. /// </summary>
  37. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  38. /// <param name="source">Source async-enumerable sequence.</param>
  39. /// <param name="predicate">A predicate function to evaluate for elements in the source sequence.</param>
  40. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  41. /// <returns>ValueTask containing the first element in the async-enumerable sequence that satisfies the condition in the predicate.</returns>
  42. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
  43. /// <exception cref="InvalidOperationException">(Asynchronous) No element satisfies the condition in the predicate. -or- The source sequence is empty.</exception>
  44. public static ValueTask<TSource> FirstAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  45. {
  46. if (source == null)
  47. throw Error.ArgumentNull(nameof(source));
  48. if (predicate == null)
  49. throw Error.ArgumentNull(nameof(predicate));
  50. return Core(source, predicate, cancellationToken);
  51. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  52. {
  53. var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
  54. return first.HasValue ? first.Value : throw Error.NoElements();
  55. }
  56. }
  57. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  58. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.firstasync?view=net-9.0-pp#system-linq-asyncenumerable-firstasync-1(system-collections-generic-iasyncenumerable((-0))-system-func((-0-system-threading-cancellationtoken-system-threading-tasks-valuetask((system-boolean))))-system-threading-cancellationtoken)
  59. // That only offers the async-with-cancellation callback overload, but the simpler form offers no additional functionality.
  60. /// <summary>
  61. /// Returns the first element of an async-enumerable sequence that satisfies the condition in the predicate.
  62. /// </summary>
  63. /// <typeparam name="TSource">The type of elements in the sequence.</typeparam>
  64. /// <param name="source">Source async-enumerable sequence.</param>
  65. /// <param name="predicate">An asynchronous predicate that will be invoked and awaited for each element in the sequence.</param>
  66. /// <param name="cancellationToken">An optional cancellation token for cancelling the sequence at any time.</param>
  67. /// <returns>A ValueTask containing the first element in the sequence that satisfies the predicate.</returns>
  68. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is <see langword="null"/>.</exception>
  69. /// <exception cref="InvalidOperationException">No element satisfies the condition in the predicate. -or- The source sequence is empty.</exception>
  70. [GenerateAsyncOverload]
  71. [Obsolete("Use FirstAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the FirstAwaitAsync functionality now exists as overloads of FirstAsync.")]
  72. private static ValueTask<TSource> FirstAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  73. {
  74. if (source == null)
  75. throw Error.ArgumentNull(nameof(source));
  76. if (predicate == null)
  77. throw Error.ArgumentNull(nameof(predicate));
  78. return Core(source, predicate, cancellationToken);
  79. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  80. {
  81. var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
  82. return first.HasValue ? first.Value : throw Error.NoElements();
  83. }
  84. }
  85. #if !NO_DEEP_CANCELLATION
  86. [GenerateAsyncOverload]
  87. [Obsolete("Use FirstAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the FirstAwaitWithCancellationAsync functionality now exists as overloads of FirstAsync.")]
  88. internal static ValueTask<TSource> FirstAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  89. {
  90. if (source == null)
  91. throw Error.ArgumentNull(nameof(source));
  92. if (predicate == null)
  93. throw Error.ArgumentNull(nameof(predicate));
  94. return Core(source, predicate, cancellationToken);
  95. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  96. {
  97. var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
  98. return first.HasValue ? first.Value : throw Error.NoElements();
  99. }
  100. }
  101. #endif
  102. }
  103. }