Count.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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<int> Count<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  12. {
  13. if (source == null)
  14. {
  15. throw new ArgumentNullException(nameof(source));
  16. }
  17. if (source is ICollection<TSource> collection)
  18. {
  19. return Task.FromResult(collection.Count);
  20. }
  21. if (source is IIListProvider<TSource> listProv)
  22. {
  23. return listProv.GetCountAsync(false, cancellationToken);
  24. }
  25. return source.Aggregate(0, (c, _) => checked(c + 1), cancellationToken);
  26. }
  27. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  28. {
  29. if (source == null)
  30. {
  31. throw new ArgumentNullException(nameof(source));
  32. }
  33. if (predicate == null)
  34. {
  35. throw new ArgumentNullException(nameof(predicate));
  36. }
  37. return source.Where(predicate)
  38. .Aggregate(0, (c, _) => checked(c + 1), cancellationToken);
  39. }
  40. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source)
  41. {
  42. if (source == null)
  43. {
  44. throw new ArgumentNullException(nameof(source));
  45. }
  46. return Count(source, CancellationToken.None);
  47. }
  48. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  49. {
  50. if (source == null)
  51. {
  52. throw new ArgumentNullException(nameof(source));
  53. }
  54. if (predicate == null)
  55. {
  56. throw new ArgumentNullException(nameof(predicate));
  57. }
  58. return Count(source, predicate, CancellationToken.None);
  59. }
  60. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  61. {
  62. if (source == null)
  63. {
  64. throw new ArgumentNullException(nameof(source));
  65. }
  66. return source.Aggregate(0L, (c, _) => checked(c + 1), cancellationToken);
  67. }
  68. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  69. {
  70. if (source == null)
  71. {
  72. throw new ArgumentNullException(nameof(source));
  73. }
  74. if (predicate == null)
  75. {
  76. throw new ArgumentNullException(nameof(predicate));
  77. }
  78. return source.Where(predicate)
  79. .Aggregate(0L, (c, _) => checked(c + 1), cancellationToken);
  80. }
  81. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source)
  82. {
  83. if (source == null)
  84. {
  85. throw new ArgumentNullException(nameof(source));
  86. }
  87. return LongCount(source, CancellationToken.None);
  88. }
  89. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  90. {
  91. if (source == null)
  92. {
  93. throw new ArgumentNullException(nameof(source));
  94. }
  95. if (predicate == null)
  96. {
  97. throw new ArgumentNullException(nameof(predicate));
  98. }
  99. return LongCount(source, predicate, CancellationToken.None);
  100. }
  101. }
  102. }