Sum.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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> Sum(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  12. {
  13. if (source == null)
  14. {
  15. throw new ArgumentNullException(nameof(source));
  16. }
  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. {
  23. throw new ArgumentNullException(nameof(source));
  24. }
  25. return source.Aggregate(0L, (x, y) => x + y, cancellationToken);
  26. }
  27. public static Task<double> Sum(this IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  28. {
  29. if (source == null)
  30. {
  31. throw new ArgumentNullException(nameof(source));
  32. }
  33. return source.Aggregate(0.0, (x, y) => x + y, cancellationToken);
  34. }
  35. public static Task<float> Sum(this IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  36. {
  37. if (source == null)
  38. {
  39. throw new ArgumentNullException(nameof(source));
  40. }
  41. return source.Aggregate(0f, (x, y) => x + y, cancellationToken);
  42. }
  43. public static Task<decimal> Sum(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  44. {
  45. if (source == null)
  46. {
  47. throw new ArgumentNullException(nameof(source));
  48. }
  49. return source.Aggregate(0m, (x, y) => x + y, cancellationToken);
  50. }
  51. public static Task<int?> Sum(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  52. {
  53. if (source == null)
  54. {
  55. throw new ArgumentNullException(nameof(source));
  56. }
  57. return source.Aggregate((int?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  58. }
  59. public static Task<long?> Sum(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  60. {
  61. if (source == null)
  62. {
  63. throw new ArgumentNullException(nameof(source));
  64. }
  65. return source.Aggregate((long?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  66. }
  67. public static Task<double?> Sum(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  68. {
  69. if (source == null)
  70. {
  71. throw new ArgumentNullException(nameof(source));
  72. }
  73. return source.Aggregate((double?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  74. }
  75. public static Task<float?> Sum(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  76. {
  77. if (source == null)
  78. {
  79. throw new ArgumentNullException(nameof(source));
  80. }
  81. return source.Aggregate((float?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  82. }
  83. public static Task<decimal?> Sum(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  84. {
  85. if (source == null)
  86. {
  87. throw new ArgumentNullException(nameof(source));
  88. }
  89. return source.Aggregate((decimal?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  90. }
  91. public static Task<int> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  92. {
  93. if (source == null)
  94. {
  95. throw new ArgumentNullException(nameof(source));
  96. }
  97. if (selector == null)
  98. {
  99. throw new ArgumentNullException(nameof(selector));
  100. }
  101. return source.Select(selector)
  102. .Sum(cancellationToken);
  103. }
  104. public static Task<long> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  105. {
  106. if (source == null)
  107. {
  108. throw new ArgumentNullException(nameof(source));
  109. }
  110. if (selector == null)
  111. {
  112. throw new ArgumentNullException(nameof(selector));
  113. }
  114. return source.Select(selector)
  115. .Sum(cancellationToken);
  116. }
  117. public static Task<double> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  118. {
  119. if (source == null)
  120. {
  121. throw new ArgumentNullException(nameof(source));
  122. }
  123. if (selector == null)
  124. {
  125. throw new ArgumentNullException(nameof(selector));
  126. }
  127. return source.Select(selector)
  128. .Sum(cancellationToken);
  129. }
  130. public static Task<float> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  131. {
  132. if (source == null)
  133. {
  134. throw new ArgumentNullException(nameof(source));
  135. }
  136. if (selector == null)
  137. {
  138. throw new ArgumentNullException(nameof(selector));
  139. }
  140. return source.Select(selector)
  141. .Sum(cancellationToken);
  142. }
  143. public static Task<decimal> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  144. {
  145. if (source == null)
  146. {
  147. throw new ArgumentNullException(nameof(source));
  148. }
  149. if (selector == null)
  150. {
  151. throw new ArgumentNullException(nameof(selector));
  152. }
  153. return source.Select(selector)
  154. .Sum(cancellationToken);
  155. }
  156. public static Task<int?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  157. {
  158. if (source == null)
  159. {
  160. throw new ArgumentNullException(nameof(source));
  161. }
  162. if (selector == null)
  163. {
  164. throw new ArgumentNullException(nameof(selector));
  165. }
  166. return source.Select(selector)
  167. .Sum(cancellationToken);
  168. }
  169. public static Task<long?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  170. {
  171. if (source == null)
  172. {
  173. throw new ArgumentNullException(nameof(source));
  174. }
  175. if (selector == null)
  176. {
  177. throw new ArgumentNullException(nameof(selector));
  178. }
  179. return source.Select(selector)
  180. .Sum(cancellationToken);
  181. }
  182. public static Task<double?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  183. {
  184. if (source == null)
  185. {
  186. throw new ArgumentNullException(nameof(source));
  187. }
  188. if (selector == null)
  189. {
  190. throw new ArgumentNullException(nameof(selector));
  191. }
  192. return source.Select(selector)
  193. .Sum(cancellationToken);
  194. }
  195. public static Task<int> Sum(this IAsyncEnumerable<int> source)
  196. {
  197. if (source == null)
  198. {
  199. throw new ArgumentNullException(nameof(source));
  200. }
  201. return Sum(source, CancellationToken.None);
  202. }
  203. public static Task<long> Sum(this IAsyncEnumerable<long> source)
  204. {
  205. if (source == null)
  206. {
  207. throw new ArgumentNullException(nameof(source));
  208. }
  209. return Sum(source, CancellationToken.None);
  210. }
  211. public static Task<double> Sum(this IAsyncEnumerable<double> source)
  212. {
  213. if (source == null)
  214. {
  215. throw new ArgumentNullException(nameof(source));
  216. }
  217. return Sum(source, CancellationToken.None);
  218. }
  219. public static Task<float> Sum(this IAsyncEnumerable<float> source)
  220. {
  221. if (source == null)
  222. {
  223. throw new ArgumentNullException(nameof(source));
  224. }
  225. return Sum(source, CancellationToken.None);
  226. }
  227. public static Task<decimal> Sum(this IAsyncEnumerable<decimal> source)
  228. {
  229. if (source == null)
  230. {
  231. throw new ArgumentNullException(nameof(source));
  232. }
  233. return Sum(source, CancellationToken.None);
  234. }
  235. public static Task<int?> Sum(this IAsyncEnumerable<int?> source)
  236. {
  237. if (source == null)
  238. {
  239. throw new ArgumentNullException(nameof(source));
  240. }
  241. return Sum(source, CancellationToken.None);
  242. }
  243. public static Task<long?> Sum(this IAsyncEnumerable<long?> source)
  244. {
  245. if (source == null)
  246. {
  247. throw new ArgumentNullException(nameof(source));
  248. }
  249. return Sum(source, CancellationToken.None);
  250. }
  251. public static Task<double?> Sum(this IAsyncEnumerable<double?> source)
  252. {
  253. if (source == null)
  254. {
  255. throw new ArgumentNullException(nameof(source));
  256. }
  257. return Sum(source, CancellationToken.None);
  258. }
  259. public static Task<float?> Sum(this IAsyncEnumerable<float?> source)
  260. {
  261. if (source == null)
  262. {
  263. throw new ArgumentNullException(nameof(source));
  264. }
  265. return Sum(source, CancellationToken.None);
  266. }
  267. public static Task<decimal?> Sum(this IAsyncEnumerable<decimal?> source)
  268. {
  269. if (source == null)
  270. {
  271. throw new ArgumentNullException(nameof(source));
  272. }
  273. return Sum(source, CancellationToken.None);
  274. }
  275. public static Task<int> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector)
  276. {
  277. if (source == null)
  278. {
  279. throw new ArgumentNullException(nameof(source));
  280. }
  281. if (selector == null)
  282. {
  283. throw new ArgumentNullException(nameof(selector));
  284. }
  285. return Sum(source, selector, CancellationToken.None);
  286. }
  287. public static Task<long> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector)
  288. {
  289. if (source == null)
  290. {
  291. throw new ArgumentNullException(nameof(source));
  292. }
  293. if (selector == null)
  294. {
  295. throw new ArgumentNullException(nameof(selector));
  296. }
  297. return Sum(source, selector, CancellationToken.None);
  298. }
  299. public static Task<double> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector)
  300. {
  301. if (source == null)
  302. {
  303. throw new ArgumentNullException(nameof(source));
  304. }
  305. if (selector == null)
  306. {
  307. throw new ArgumentNullException(nameof(selector));
  308. }
  309. return Sum(source, selector, CancellationToken.None);
  310. }
  311. public static Task<float> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector)
  312. {
  313. if (source == null)
  314. {
  315. throw new ArgumentNullException(nameof(source));
  316. }
  317. if (selector == null)
  318. {
  319. throw new ArgumentNullException(nameof(selector));
  320. }
  321. return Sum(source, selector, CancellationToken.None);
  322. }
  323. public static Task<decimal> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector)
  324. {
  325. if (source == null)
  326. {
  327. throw new ArgumentNullException(nameof(source));
  328. }
  329. if (selector == null)
  330. {
  331. throw new ArgumentNullException(nameof(selector));
  332. }
  333. return Sum(source, selector, CancellationToken.None);
  334. }
  335. public static Task<int?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector)
  336. {
  337. if (source == null)
  338. {
  339. throw new ArgumentNullException(nameof(source));
  340. }
  341. if (selector == null)
  342. {
  343. throw new ArgumentNullException(nameof(selector));
  344. }
  345. return Sum(source, selector, CancellationToken.None);
  346. }
  347. public static Task<long?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector)
  348. {
  349. if (source == null)
  350. {
  351. throw new ArgumentNullException(nameof(source));
  352. }
  353. if (selector == null)
  354. {
  355. throw new ArgumentNullException(nameof(selector));
  356. }
  357. return Sum(source, selector, CancellationToken.None);
  358. }
  359. public static Task<double?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector)
  360. {
  361. if (source == null)
  362. {
  363. throw new ArgumentNullException(nameof(source));
  364. }
  365. if (selector == null)
  366. {
  367. throw new ArgumentNullException(nameof(selector));
  368. }
  369. return Sum(source, selector, CancellationToken.None);
  370. }
  371. public static Task<float?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector)
  372. {
  373. if (source == null)
  374. {
  375. throw new ArgumentNullException(nameof(source));
  376. }
  377. if (selector == null)
  378. {
  379. throw new ArgumentNullException(nameof(selector));
  380. }
  381. return Sum(source, selector, CancellationToken.None);
  382. }
  383. public static Task<decimal?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector)
  384. {
  385. if (source == null)
  386. {
  387. throw new ArgumentNullException(nameof(source));
  388. }
  389. if (selector == null)
  390. {
  391. throw new ArgumentNullException(nameof(selector));
  392. }
  393. return Sum(source, selector, CancellationToken.None);
  394. }
  395. public static Task<float?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  396. {
  397. if (source == null)
  398. {
  399. throw new ArgumentNullException(nameof(source));
  400. }
  401. if (selector == null)
  402. {
  403. throw new ArgumentNullException(nameof(selector));
  404. }
  405. return source.Select(selector)
  406. .Sum(cancellationToken);
  407. }
  408. public static Task<decimal?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  409. {
  410. if (source == null)
  411. {
  412. throw new ArgumentNullException(nameof(source));
  413. }
  414. if (selector == null)
  415. {
  416. throw new ArgumentNullException(nameof(selector));
  417. }
  418. return source.Select(selector)
  419. .Sum(cancellationToken);
  420. }
  421. }
  422. }