Count.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. return source.Aggregate(0, (c, _) => checked(c + 1), cancellationToken);
  18. }
  19. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. if (predicate == null)
  24. throw new ArgumentNullException(nameof(predicate));
  25. return source.Where(predicate)
  26. .Aggregate(0, (c, _) => checked(c + 1), cancellationToken);
  27. }
  28. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source)
  29. {
  30. if (source == null)
  31. throw new ArgumentNullException(nameof(source));
  32. return Count(source, CancellationToken.None);
  33. }
  34. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  35. {
  36. if (source == null)
  37. throw new ArgumentNullException(nameof(source));
  38. if (predicate == null)
  39. throw new ArgumentNullException(nameof(predicate));
  40. return Count(source, predicate, CancellationToken.None);
  41. }
  42. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  43. {
  44. if (source == null)
  45. throw new ArgumentNullException(nameof(source));
  46. return source.Aggregate(0L, (c, _) => checked(c + 1), cancellationToken);
  47. }
  48. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, 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. .Aggregate(0L, (c, _) => checked(c + 1), cancellationToken);
  56. }
  57. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source)
  58. {
  59. if (source == null)
  60. throw new ArgumentNullException(nameof(source));
  61. return LongCount(source, CancellationToken.None);
  62. }
  63. public static Task<long> LongCount<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 LongCount(source, predicate, CancellationToken.None);
  70. }
  71. }
  72. }