First.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /// <summary>
  12. /// Returns the first element of an async-enumerable sequence.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">Source async-enumerable sequence.</param>
  16. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  17. /// <returns>ValueTask containing the first element in the async-enumerable sequence.</returns>
  18. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  19. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  20. public static ValueTask<TSource> FirstAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  21. {
  22. if (source == null)
  23. throw Error.ArgumentNull(nameof(source));
  24. return Core(source, cancellationToken);
  25. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  26. {
  27. var first = await TryGetFirst(source, cancellationToken).ConfigureAwait(false);
  28. return first.HasValue ? first.Value : throw Error.NoElements();
  29. }
  30. }
  31. /// <summary>
  32. /// Returns the first element of an async-enumerable sequence that satisfies the condition in the predicate.
  33. /// </summary>
  34. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  35. /// <param name="source">Source async-enumerable sequence.</param>
  36. /// <param name="predicate">A predicate function to evaluate for elements in the source sequence.</param>
  37. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  38. /// <returns>ValueTask containing the first element in the async-enumerable sequence that satisfies the condition in the predicate.</returns>
  39. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
  40. /// <exception cref="InvalidOperationException">(Asynchronous) No element satisfies the condition in the predicate. -or- The source sequence is empty.</exception>
  41. public static ValueTask<TSource> FirstAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  42. {
  43. if (source == null)
  44. throw Error.ArgumentNull(nameof(source));
  45. if (predicate == null)
  46. throw Error.ArgumentNull(nameof(predicate));
  47. return Core(source, predicate, cancellationToken);
  48. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  49. {
  50. var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
  51. return first.HasValue ? first.Value : throw Error.NoElements();
  52. }
  53. }
  54. internal static ValueTask<TSource> FirstAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  55. {
  56. if (source == null)
  57. throw Error.ArgumentNull(nameof(source));
  58. if (predicate == null)
  59. throw Error.ArgumentNull(nameof(predicate));
  60. return Core(source, predicate, cancellationToken);
  61. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  62. {
  63. var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
  64. return first.HasValue ? first.Value : throw Error.NoElements();
  65. }
  66. }
  67. #if !NO_DEEP_CANCELLATION
  68. internal static ValueTask<TSource> FirstAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  69. {
  70. if (source == null)
  71. throw Error.ArgumentNull(nameof(source));
  72. if (predicate == null)
  73. throw Error.ArgumentNull(nameof(predicate));
  74. return Core(source, predicate, cancellationToken);
  75. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  76. {
  77. var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false);
  78. return first.HasValue ? first.Value : throw Error.NoElements();
  79. }
  80. }
  81. #endif
  82. }
  83. }