Sum.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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> Sum(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. return source.Aggregate(0, (x, y) => x + y, cancellationToken);
  18. }
  19. public static Task<long> Sum(this IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. return source.Aggregate(0L, (x, y) => x + y, cancellationToken);
  24. }
  25. public static Task<double> Sum(this IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  26. {
  27. if (source == null)
  28. throw new ArgumentNullException(nameof(source));
  29. return source.Aggregate(0.0, (x, y) => x + y, cancellationToken);
  30. }
  31. public static Task<float> Sum(this IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  32. {
  33. if (source == null)
  34. throw new ArgumentNullException(nameof(source));
  35. return source.Aggregate(0f, (x, y) => x + y, cancellationToken);
  36. }
  37. public static Task<decimal> Sum(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  38. {
  39. if (source == null)
  40. throw new ArgumentNullException(nameof(source));
  41. return source.Aggregate(0m, (x, y) => x + y, cancellationToken);
  42. }
  43. public static Task<int?> Sum(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  44. {
  45. if (source == null)
  46. throw new ArgumentNullException(nameof(source));
  47. return source.Aggregate((int?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  48. }
  49. public static Task<long?> Sum(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  50. {
  51. if (source == null)
  52. throw new ArgumentNullException(nameof(source));
  53. return source.Aggregate((long?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  54. }
  55. public static Task<double?> Sum(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  56. {
  57. if (source == null)
  58. throw new ArgumentNullException(nameof(source));
  59. return source.Aggregate((double?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  60. }
  61. public static Task<float?> Sum(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  62. {
  63. if (source == null)
  64. throw new ArgumentNullException(nameof(source));
  65. return source.Aggregate((float?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  66. }
  67. public static Task<decimal?> Sum(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  68. {
  69. if (source == null)
  70. throw new ArgumentNullException(nameof(source));
  71. return source.Aggregate((decimal?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  72. }
  73. public static Task<int> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  74. {
  75. if (source == null)
  76. throw new ArgumentNullException(nameof(source));
  77. if (selector == null)
  78. throw new ArgumentNullException(nameof(selector));
  79. return source.Select(selector)
  80. .Sum(cancellationToken);
  81. }
  82. public static Task<long> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  83. {
  84. if (source == null)
  85. throw new ArgumentNullException(nameof(source));
  86. if (selector == null)
  87. throw new ArgumentNullException(nameof(selector));
  88. return source.Select(selector)
  89. .Sum(cancellationToken);
  90. }
  91. public static Task<double> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  92. {
  93. if (source == null)
  94. throw new ArgumentNullException(nameof(source));
  95. if (selector == null)
  96. throw new ArgumentNullException(nameof(selector));
  97. return source.Select(selector)
  98. .Sum(cancellationToken);
  99. }
  100. public static Task<float> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  101. {
  102. if (source == null)
  103. throw new ArgumentNullException(nameof(source));
  104. if (selector == null)
  105. throw new ArgumentNullException(nameof(selector));
  106. return source.Select(selector)
  107. .Sum(cancellationToken);
  108. }
  109. public static Task<decimal> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  110. {
  111. if (source == null)
  112. throw new ArgumentNullException(nameof(source));
  113. if (selector == null)
  114. throw new ArgumentNullException(nameof(selector));
  115. return source.Select(selector)
  116. .Sum(cancellationToken);
  117. }
  118. public static Task<int?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  119. {
  120. if (source == null)
  121. throw new ArgumentNullException(nameof(source));
  122. if (selector == null)
  123. throw new ArgumentNullException(nameof(selector));
  124. return source.Select(selector)
  125. .Sum(cancellationToken);
  126. }
  127. public static Task<long?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  128. {
  129. if (source == null)
  130. throw new ArgumentNullException(nameof(source));
  131. if (selector == null)
  132. throw new ArgumentNullException(nameof(selector));
  133. return source.Select(selector)
  134. .Sum(cancellationToken);
  135. }
  136. public static Task<double?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  137. {
  138. if (source == null)
  139. throw new ArgumentNullException(nameof(source));
  140. if (selector == null)
  141. throw new ArgumentNullException(nameof(selector));
  142. return source.Select(selector)
  143. .Sum(cancellationToken);
  144. }
  145. public static Task<int> Sum(this IAsyncEnumerable<int> source)
  146. {
  147. if (source == null)
  148. throw new ArgumentNullException(nameof(source));
  149. return Sum(source, CancellationToken.None);
  150. }
  151. public static Task<long> Sum(this IAsyncEnumerable<long> source)
  152. {
  153. if (source == null)
  154. throw new ArgumentNullException(nameof(source));
  155. return Sum(source, CancellationToken.None);
  156. }
  157. public static Task<double> Sum(this IAsyncEnumerable<double> source)
  158. {
  159. if (source == null)
  160. throw new ArgumentNullException(nameof(source));
  161. return Sum(source, CancellationToken.None);
  162. }
  163. public static Task<float> Sum(this IAsyncEnumerable<float> source)
  164. {
  165. if (source == null)
  166. throw new ArgumentNullException(nameof(source));
  167. return Sum(source, CancellationToken.None);
  168. }
  169. public static Task<decimal> Sum(this IAsyncEnumerable<decimal> source)
  170. {
  171. if (source == null)
  172. throw new ArgumentNullException(nameof(source));
  173. return Sum(source, CancellationToken.None);
  174. }
  175. public static Task<int?> Sum(this IAsyncEnumerable<int?> source)
  176. {
  177. if (source == null)
  178. throw new ArgumentNullException(nameof(source));
  179. return Sum(source, CancellationToken.None);
  180. }
  181. public static Task<long?> Sum(this IAsyncEnumerable<long?> source)
  182. {
  183. if (source == null)
  184. throw new ArgumentNullException(nameof(source));
  185. return Sum(source, CancellationToken.None);
  186. }
  187. public static Task<double?> Sum(this IAsyncEnumerable<double?> source)
  188. {
  189. if (source == null)
  190. throw new ArgumentNullException(nameof(source));
  191. return Sum(source, CancellationToken.None);
  192. }
  193. public static Task<float?> Sum(this IAsyncEnumerable<float?> source)
  194. {
  195. if (source == null)
  196. throw new ArgumentNullException(nameof(source));
  197. return Sum(source, CancellationToken.None);
  198. }
  199. public static Task<decimal?> Sum(this IAsyncEnumerable<decimal?> source)
  200. {
  201. if (source == null)
  202. throw new ArgumentNullException(nameof(source));
  203. return Sum(source, CancellationToken.None);
  204. }
  205. public static Task<int> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector)
  206. {
  207. if (source == null)
  208. throw new ArgumentNullException(nameof(source));
  209. if (selector == null)
  210. throw new ArgumentNullException(nameof(selector));
  211. return Sum(source, selector, CancellationToken.None);
  212. }
  213. public static Task<long> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector)
  214. {
  215. if (source == null)
  216. throw new ArgumentNullException(nameof(source));
  217. if (selector == null)
  218. throw new ArgumentNullException(nameof(selector));
  219. return Sum(source, selector, CancellationToken.None);
  220. }
  221. public static Task<double> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector)
  222. {
  223. if (source == null)
  224. throw new ArgumentNullException(nameof(source));
  225. if (selector == null)
  226. throw new ArgumentNullException(nameof(selector));
  227. return Sum(source, selector, CancellationToken.None);
  228. }
  229. public static Task<float> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector)
  230. {
  231. if (source == null)
  232. throw new ArgumentNullException(nameof(source));
  233. if (selector == null)
  234. throw new ArgumentNullException(nameof(selector));
  235. return Sum(source, selector, CancellationToken.None);
  236. }
  237. public static Task<decimal> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector)
  238. {
  239. if (source == null)
  240. throw new ArgumentNullException(nameof(source));
  241. if (selector == null)
  242. throw new ArgumentNullException(nameof(selector));
  243. return Sum(source, selector, CancellationToken.None);
  244. }
  245. public static Task<int?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector)
  246. {
  247. if (source == null)
  248. throw new ArgumentNullException(nameof(source));
  249. if (selector == null)
  250. throw new ArgumentNullException(nameof(selector));
  251. return Sum(source, selector, CancellationToken.None);
  252. }
  253. public static Task<long?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector)
  254. {
  255. if (source == null)
  256. throw new ArgumentNullException(nameof(source));
  257. if (selector == null)
  258. throw new ArgumentNullException(nameof(selector));
  259. return Sum(source, selector, CancellationToken.None);
  260. }
  261. public static Task<double?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector)
  262. {
  263. if (source == null)
  264. throw new ArgumentNullException(nameof(source));
  265. if (selector == null)
  266. throw new ArgumentNullException(nameof(selector));
  267. return Sum(source, selector, CancellationToken.None);
  268. }
  269. public static Task<float?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector)
  270. {
  271. if (source == null)
  272. throw new ArgumentNullException(nameof(source));
  273. if (selector == null)
  274. throw new ArgumentNullException(nameof(selector));
  275. return Sum(source, selector, CancellationToken.None);
  276. }
  277. public static Task<decimal?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector)
  278. {
  279. if (source == null)
  280. throw new ArgumentNullException(nameof(source));
  281. if (selector == null)
  282. throw new ArgumentNullException(nameof(selector));
  283. return Sum(source, selector, CancellationToken.None);
  284. }
  285. public static Task<float?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  286. {
  287. if (source == null)
  288. throw new ArgumentNullException(nameof(source));
  289. if (selector == null)
  290. throw new ArgumentNullException(nameof(selector));
  291. return source.Select(selector)
  292. .Sum(cancellationToken);
  293. }
  294. public static Task<decimal?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  295. {
  296. if (source == null)
  297. throw new ArgumentNullException(nameof(source));
  298. if (selector == null)
  299. throw new ArgumentNullException(nameof(selector));
  300. return source.Select(selector)
  301. .Sum(cancellationToken);
  302. }
  303. }
  304. }