Count.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. var collectionoft = source as ICollection<TSource>;
  18. if (collectionoft != null)
  19. {
  20. return Task.FromResult(collectionoft.Count);
  21. }
  22. var listProv = source as IIListProvider<TSource>;
  23. if (listProv != null)
  24. {
  25. return listProv.GetCountAsync(onlyIfCheap: false, cancellationToken: cancellationToken);
  26. }
  27. return source.Aggregate(0, (c, _) => checked(c + 1), cancellationToken);
  28. }
  29. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  30. {
  31. if (source == null)
  32. throw new ArgumentNullException(nameof(source));
  33. if (predicate == null)
  34. throw new ArgumentNullException(nameof(predicate));
  35. return source.Where(predicate)
  36. .Aggregate(0, (c, _) => checked(c + 1), cancellationToken);
  37. }
  38. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source)
  39. {
  40. if (source == null)
  41. throw new ArgumentNullException(nameof(source));
  42. return Count(source, CancellationToken.None);
  43. }
  44. public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  45. {
  46. if (source == null)
  47. throw new ArgumentNullException(nameof(source));
  48. if (predicate == null)
  49. throw new ArgumentNullException(nameof(predicate));
  50. return Count(source, predicate, CancellationToken.None);
  51. }
  52. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  53. {
  54. if (source == null)
  55. throw new ArgumentNullException(nameof(source));
  56. return source.Aggregate(0L, (c, _) => checked(c + 1), cancellationToken);
  57. }
  58. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  59. {
  60. if (source == null)
  61. throw new ArgumentNullException(nameof(source));
  62. if (predicate == null)
  63. throw new ArgumentNullException(nameof(predicate));
  64. return source.Where(predicate)
  65. .Aggregate(0L, (c, _) => checked(c + 1), cancellationToken);
  66. }
  67. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source)
  68. {
  69. if (source == null)
  70. throw new ArgumentNullException(nameof(source));
  71. return LongCount(source, CancellationToken.None);
  72. }
  73. public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  74. {
  75. if (source == null)
  76. throw new ArgumentNullException(nameof(source));
  77. if (predicate == null)
  78. throw new ArgumentNullException(nameof(predicate));
  79. return LongCount(source, predicate, CancellationToken.None);
  80. }
  81. }
  82. }