First.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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> FirstAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  12. {
  13. if (source == null)
  14. throw Error.ArgumentNull(nameof(source));
  15. return Core(source, cancellationToken);
  16. async Task<TSource> Core(IAsyncEnumerable<TSource> _source, CancellationToken _cancellationToken)
  17. {
  18. var first = await TryGetFirst(_source, _cancellationToken).ConfigureAwait(false);
  19. return first.HasValue ? first.Value : throw Error.NoElements();
  20. }
  21. }
  22. public static Task<TSource> FirstAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  23. {
  24. if (source == null)
  25. throw Error.ArgumentNull(nameof(source));
  26. if (predicate == null)
  27. throw Error.ArgumentNull(nameof(predicate));
  28. return Core(source, predicate, cancellationToken);
  29. async Task<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, bool> _predicate, CancellationToken _cancellationToken)
  30. {
  31. var first = await TryGetFirst(_source, _predicate, _cancellationToken).ConfigureAwait(false);
  32. return first.HasValue ? first.Value : throw Error.NoElements();
  33. }
  34. }
  35. public static Task<TSource> FirstAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  36. {
  37. if (source == null)
  38. throw Error.ArgumentNull(nameof(source));
  39. if (predicate == null)
  40. throw Error.ArgumentNull(nameof(predicate));
  41. return Core(source, predicate, cancellationToken);
  42. async Task<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  43. {
  44. var first = await TryGetFirst(_source, _predicate, _cancellationToken).ConfigureAwait(false);
  45. return first.HasValue ? first.Value : throw Error.NoElements();
  46. }
  47. }
  48. #if !NO_DEEP_CANCELLATION
  49. public static Task<TSource> FirstAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  50. {
  51. if (source == null)
  52. throw Error.ArgumentNull(nameof(source));
  53. if (predicate == null)
  54. throw Error.ArgumentNull(nameof(predicate));
  55. return Core(source, predicate, cancellationToken);
  56. async Task<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  57. {
  58. var first = await TryGetFirst(_source, _predicate, _cancellationToken).ConfigureAwait(false);
  59. return first.HasValue ? first.Value : throw Error.NoElements();
  60. }
  61. }
  62. #endif
  63. }
  64. }