Sum.Generated.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. return SumCore(source, CancellationToken.None);
  16. }
  17. public static Task<int> Sum(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  18. {
  19. if (source == null)
  20. throw new ArgumentNullException(nameof(source));
  21. return SumCore(source, cancellationToken);
  22. }
  23. public static Task<int> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector)
  24. {
  25. if (source == null)
  26. throw new ArgumentNullException(nameof(source));
  27. if (selector == null)
  28. throw new ArgumentNullException(nameof(selector));
  29. return SumCore(source, selector, CancellationToken.None);
  30. }
  31. public static Task<int> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  32. {
  33. if (source == null)
  34. throw new ArgumentNullException(nameof(source));
  35. if (selector == null)
  36. throw new ArgumentNullException(nameof(selector));
  37. return SumCore(source, selector, cancellationToken);
  38. }
  39. public static Task<int> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<int>> selector)
  40. {
  41. if (source == null)
  42. throw new ArgumentNullException(nameof(source));
  43. if (selector == null)
  44. throw new ArgumentNullException(nameof(selector));
  45. return SumCore(source, selector, CancellationToken.None);
  46. }
  47. public static Task<int> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<int>> selector, CancellationToken cancellationToken)
  48. {
  49. if (source == null)
  50. throw new ArgumentNullException(nameof(source));
  51. if (selector == null)
  52. throw new ArgumentNullException(nameof(selector));
  53. return SumCore(source, selector, cancellationToken);
  54. }
  55. private static Task<int> SumCore(IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  56. {
  57. return source.Aggregate(0, (x, y) => x + y, cancellationToken);
  58. }
  59. private static Task<int> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  60. {
  61. return source.Select(selector).Sum(cancellationToken);
  62. }
  63. private static Task<int> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<int>> selector, CancellationToken cancellationToken)
  64. {
  65. return source.Select(selector).Sum(cancellationToken);
  66. }
  67. public static Task<long> Sum(this IAsyncEnumerable<long> source)
  68. {
  69. if (source == null)
  70. throw new ArgumentNullException(nameof(source));
  71. return SumCore(source, CancellationToken.None);
  72. }
  73. public static Task<long> Sum(this IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  74. {
  75. if (source == null)
  76. throw new ArgumentNullException(nameof(source));
  77. return SumCore(source, cancellationToken);
  78. }
  79. public static Task<long> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector)
  80. {
  81. if (source == null)
  82. throw new ArgumentNullException(nameof(source));
  83. if (selector == null)
  84. throw new ArgumentNullException(nameof(selector));
  85. return SumCore(source, selector, CancellationToken.None);
  86. }
  87. public static Task<long> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  88. {
  89. if (source == null)
  90. throw new ArgumentNullException(nameof(source));
  91. if (selector == null)
  92. throw new ArgumentNullException(nameof(selector));
  93. return SumCore(source, selector, cancellationToken);
  94. }
  95. public static Task<long> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<long>> selector)
  96. {
  97. if (source == null)
  98. throw new ArgumentNullException(nameof(source));
  99. if (selector == null)
  100. throw new ArgumentNullException(nameof(selector));
  101. return SumCore(source, selector, CancellationToken.None);
  102. }
  103. public static Task<long> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<long>> selector, CancellationToken cancellationToken)
  104. {
  105. if (source == null)
  106. throw new ArgumentNullException(nameof(source));
  107. if (selector == null)
  108. throw new ArgumentNullException(nameof(selector));
  109. return SumCore(source, selector, cancellationToken);
  110. }
  111. private static Task<long> SumCore(IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  112. {
  113. return source.Aggregate(0L, (x, y) => x + y, cancellationToken);
  114. }
  115. private static Task<long> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  116. {
  117. return source.Select(selector).Sum(cancellationToken);
  118. }
  119. private static Task<long> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<long>> selector, CancellationToken cancellationToken)
  120. {
  121. return source.Select(selector).Sum(cancellationToken);
  122. }
  123. public static Task<float> Sum(this IAsyncEnumerable<float> source)
  124. {
  125. if (source == null)
  126. throw new ArgumentNullException(nameof(source));
  127. return SumCore(source, CancellationToken.None);
  128. }
  129. public static Task<float> Sum(this IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  130. {
  131. if (source == null)
  132. throw new ArgumentNullException(nameof(source));
  133. return SumCore(source, cancellationToken);
  134. }
  135. public static Task<float> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector)
  136. {
  137. if (source == null)
  138. throw new ArgumentNullException(nameof(source));
  139. if (selector == null)
  140. throw new ArgumentNullException(nameof(selector));
  141. return SumCore(source, selector, CancellationToken.None);
  142. }
  143. public static Task<float> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  144. {
  145. if (source == null)
  146. throw new ArgumentNullException(nameof(source));
  147. if (selector == null)
  148. throw new ArgumentNullException(nameof(selector));
  149. return SumCore(source, selector, cancellationToken);
  150. }
  151. public static Task<float> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<float>> selector)
  152. {
  153. if (source == null)
  154. throw new ArgumentNullException(nameof(source));
  155. if (selector == null)
  156. throw new ArgumentNullException(nameof(selector));
  157. return SumCore(source, selector, CancellationToken.None);
  158. }
  159. public static Task<float> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<float>> selector, CancellationToken cancellationToken)
  160. {
  161. if (source == null)
  162. throw new ArgumentNullException(nameof(source));
  163. if (selector == null)
  164. throw new ArgumentNullException(nameof(selector));
  165. return SumCore(source, selector, cancellationToken);
  166. }
  167. private static Task<float> SumCore(IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  168. {
  169. return source.Aggregate(0.0f, (x, y) => x + y, cancellationToken);
  170. }
  171. private static Task<float> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  172. {
  173. return source.Select(selector).Sum(cancellationToken);
  174. }
  175. private static Task<float> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<float>> selector, CancellationToken cancellationToken)
  176. {
  177. return source.Select(selector).Sum(cancellationToken);
  178. }
  179. public static Task<double> Sum(this IAsyncEnumerable<double> source)
  180. {
  181. if (source == null)
  182. throw new ArgumentNullException(nameof(source));
  183. return SumCore(source, CancellationToken.None);
  184. }
  185. public static Task<double> Sum(this IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  186. {
  187. if (source == null)
  188. throw new ArgumentNullException(nameof(source));
  189. return SumCore(source, cancellationToken);
  190. }
  191. public static Task<double> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector)
  192. {
  193. if (source == null)
  194. throw new ArgumentNullException(nameof(source));
  195. if (selector == null)
  196. throw new ArgumentNullException(nameof(selector));
  197. return SumCore(source, selector, CancellationToken.None);
  198. }
  199. public static Task<double> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  200. {
  201. if (source == null)
  202. throw new ArgumentNullException(nameof(source));
  203. if (selector == null)
  204. throw new ArgumentNullException(nameof(selector));
  205. return SumCore(source, selector, cancellationToken);
  206. }
  207. public static Task<double> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<double>> selector)
  208. {
  209. if (source == null)
  210. throw new ArgumentNullException(nameof(source));
  211. if (selector == null)
  212. throw new ArgumentNullException(nameof(selector));
  213. return SumCore(source, selector, CancellationToken.None);
  214. }
  215. public static Task<double> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<double>> selector, CancellationToken cancellationToken)
  216. {
  217. if (source == null)
  218. throw new ArgumentNullException(nameof(source));
  219. if (selector == null)
  220. throw new ArgumentNullException(nameof(selector));
  221. return SumCore(source, selector, cancellationToken);
  222. }
  223. private static Task<double> SumCore(IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  224. {
  225. return source.Aggregate(0.0, (x, y) => x + y, cancellationToken);
  226. }
  227. private static Task<double> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  228. {
  229. return source.Select(selector).Sum(cancellationToken);
  230. }
  231. private static Task<double> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<double>> selector, CancellationToken cancellationToken)
  232. {
  233. return source.Select(selector).Sum(cancellationToken);
  234. }
  235. public static Task<decimal> Sum(this IAsyncEnumerable<decimal> source)
  236. {
  237. if (source == null)
  238. throw new ArgumentNullException(nameof(source));
  239. return SumCore(source, CancellationToken.None);
  240. }
  241. public static Task<decimal> Sum(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  242. {
  243. if (source == null)
  244. throw new ArgumentNullException(nameof(source));
  245. return SumCore(source, cancellationToken);
  246. }
  247. public static Task<decimal> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector)
  248. {
  249. if (source == null)
  250. throw new ArgumentNullException(nameof(source));
  251. if (selector == null)
  252. throw new ArgumentNullException(nameof(selector));
  253. return SumCore(source, selector, CancellationToken.None);
  254. }
  255. public static Task<decimal> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  256. {
  257. if (source == null)
  258. throw new ArgumentNullException(nameof(source));
  259. if (selector == null)
  260. throw new ArgumentNullException(nameof(selector));
  261. return SumCore(source, selector, cancellationToken);
  262. }
  263. public static Task<decimal> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<decimal>> selector)
  264. {
  265. if (source == null)
  266. throw new ArgumentNullException(nameof(source));
  267. if (selector == null)
  268. throw new ArgumentNullException(nameof(selector));
  269. return SumCore(source, selector, CancellationToken.None);
  270. }
  271. public static Task<decimal> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<decimal>> selector, CancellationToken cancellationToken)
  272. {
  273. if (source == null)
  274. throw new ArgumentNullException(nameof(source));
  275. if (selector == null)
  276. throw new ArgumentNullException(nameof(selector));
  277. return SumCore(source, selector, cancellationToken);
  278. }
  279. private static Task<decimal> SumCore(IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  280. {
  281. return source.Aggregate(0m, (x, y) => x + y, cancellationToken);
  282. }
  283. private static Task<decimal> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  284. {
  285. return source.Select(selector).Sum(cancellationToken);
  286. }
  287. private static Task<decimal> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<decimal>> selector, CancellationToken cancellationToken)
  288. {
  289. return source.Select(selector).Sum(cancellationToken);
  290. }
  291. public static Task<int?> Sum(this IAsyncEnumerable<int?> source)
  292. {
  293. if (source == null)
  294. throw new ArgumentNullException(nameof(source));
  295. return SumCore(source, CancellationToken.None);
  296. }
  297. public static Task<int?> Sum(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  298. {
  299. if (source == null)
  300. throw new ArgumentNullException(nameof(source));
  301. return SumCore(source, cancellationToken);
  302. }
  303. public static Task<int?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector)
  304. {
  305. if (source == null)
  306. throw new ArgumentNullException(nameof(source));
  307. if (selector == null)
  308. throw new ArgumentNullException(nameof(selector));
  309. return SumCore(source, selector, CancellationToken.None);
  310. }
  311. public static Task<int?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  312. {
  313. if (source == null)
  314. throw new ArgumentNullException(nameof(source));
  315. if (selector == null)
  316. throw new ArgumentNullException(nameof(selector));
  317. return SumCore(source, selector, cancellationToken);
  318. }
  319. public static Task<int?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<int?>> selector)
  320. {
  321. if (source == null)
  322. throw new ArgumentNullException(nameof(source));
  323. if (selector == null)
  324. throw new ArgumentNullException(nameof(selector));
  325. return SumCore(source, selector, CancellationToken.None);
  326. }
  327. public static Task<int?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<int?>> selector, CancellationToken cancellationToken)
  328. {
  329. if (source == null)
  330. throw new ArgumentNullException(nameof(source));
  331. if (selector == null)
  332. throw new ArgumentNullException(nameof(selector));
  333. return SumCore(source, selector, cancellationToken);
  334. }
  335. private static Task<int?> SumCore(IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  336. {
  337. return source.Aggregate((int?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  338. }
  339. private static Task<int?> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  340. {
  341. return source.Select(selector).Sum(cancellationToken);
  342. }
  343. private static Task<int?> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<int?>> selector, CancellationToken cancellationToken)
  344. {
  345. return source.Select(selector).Sum(cancellationToken);
  346. }
  347. public static Task<long?> Sum(this IAsyncEnumerable<long?> source)
  348. {
  349. if (source == null)
  350. throw new ArgumentNullException(nameof(source));
  351. return SumCore(source, CancellationToken.None);
  352. }
  353. public static Task<long?> Sum(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  354. {
  355. if (source == null)
  356. throw new ArgumentNullException(nameof(source));
  357. return SumCore(source, cancellationToken);
  358. }
  359. public static Task<long?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector)
  360. {
  361. if (source == null)
  362. throw new ArgumentNullException(nameof(source));
  363. if (selector == null)
  364. throw new ArgumentNullException(nameof(selector));
  365. return SumCore(source, selector, CancellationToken.None);
  366. }
  367. public static Task<long?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  368. {
  369. if (source == null)
  370. throw new ArgumentNullException(nameof(source));
  371. if (selector == null)
  372. throw new ArgumentNullException(nameof(selector));
  373. return SumCore(source, selector, cancellationToken);
  374. }
  375. public static Task<long?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<long?>> selector)
  376. {
  377. if (source == null)
  378. throw new ArgumentNullException(nameof(source));
  379. if (selector == null)
  380. throw new ArgumentNullException(nameof(selector));
  381. return SumCore(source, selector, CancellationToken.None);
  382. }
  383. public static Task<long?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<long?>> selector, CancellationToken cancellationToken)
  384. {
  385. if (source == null)
  386. throw new ArgumentNullException(nameof(source));
  387. if (selector == null)
  388. throw new ArgumentNullException(nameof(selector));
  389. return SumCore(source, selector, cancellationToken);
  390. }
  391. private static Task<long?> SumCore(IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  392. {
  393. return source.Aggregate((long?)0L, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  394. }
  395. private static Task<long?> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  396. {
  397. return source.Select(selector).Sum(cancellationToken);
  398. }
  399. private static Task<long?> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<long?>> selector, CancellationToken cancellationToken)
  400. {
  401. return source.Select(selector).Sum(cancellationToken);
  402. }
  403. public static Task<float?> Sum(this IAsyncEnumerable<float?> source)
  404. {
  405. if (source == null)
  406. throw new ArgumentNullException(nameof(source));
  407. return SumCore(source, CancellationToken.None);
  408. }
  409. public static Task<float?> Sum(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  410. {
  411. if (source == null)
  412. throw new ArgumentNullException(nameof(source));
  413. return SumCore(source, cancellationToken);
  414. }
  415. public static Task<float?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector)
  416. {
  417. if (source == null)
  418. throw new ArgumentNullException(nameof(source));
  419. if (selector == null)
  420. throw new ArgumentNullException(nameof(selector));
  421. return SumCore(source, selector, CancellationToken.None);
  422. }
  423. public static Task<float?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  424. {
  425. if (source == null)
  426. throw new ArgumentNullException(nameof(source));
  427. if (selector == null)
  428. throw new ArgumentNullException(nameof(selector));
  429. return SumCore(source, selector, cancellationToken);
  430. }
  431. public static Task<float?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<float?>> selector)
  432. {
  433. if (source == null)
  434. throw new ArgumentNullException(nameof(source));
  435. if (selector == null)
  436. throw new ArgumentNullException(nameof(selector));
  437. return SumCore(source, selector, CancellationToken.None);
  438. }
  439. public static Task<float?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<float?>> selector, CancellationToken cancellationToken)
  440. {
  441. if (source == null)
  442. throw new ArgumentNullException(nameof(source));
  443. if (selector == null)
  444. throw new ArgumentNullException(nameof(selector));
  445. return SumCore(source, selector, cancellationToken);
  446. }
  447. private static Task<float?> SumCore(IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  448. {
  449. return source.Aggregate((float?)0.0f, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  450. }
  451. private static Task<float?> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  452. {
  453. return source.Select(selector).Sum(cancellationToken);
  454. }
  455. private static Task<float?> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<float?>> selector, CancellationToken cancellationToken)
  456. {
  457. return source.Select(selector).Sum(cancellationToken);
  458. }
  459. public static Task<double?> Sum(this IAsyncEnumerable<double?> source)
  460. {
  461. if (source == null)
  462. throw new ArgumentNullException(nameof(source));
  463. return SumCore(source, CancellationToken.None);
  464. }
  465. public static Task<double?> Sum(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  466. {
  467. if (source == null)
  468. throw new ArgumentNullException(nameof(source));
  469. return SumCore(source, cancellationToken);
  470. }
  471. public static Task<double?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector)
  472. {
  473. if (source == null)
  474. throw new ArgumentNullException(nameof(source));
  475. if (selector == null)
  476. throw new ArgumentNullException(nameof(selector));
  477. return SumCore(source, selector, CancellationToken.None);
  478. }
  479. public static Task<double?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  480. {
  481. if (source == null)
  482. throw new ArgumentNullException(nameof(source));
  483. if (selector == null)
  484. throw new ArgumentNullException(nameof(selector));
  485. return SumCore(source, selector, cancellationToken);
  486. }
  487. public static Task<double?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<double?>> selector)
  488. {
  489. if (source == null)
  490. throw new ArgumentNullException(nameof(source));
  491. if (selector == null)
  492. throw new ArgumentNullException(nameof(selector));
  493. return SumCore(source, selector, CancellationToken.None);
  494. }
  495. public static Task<double?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<double?>> selector, CancellationToken cancellationToken)
  496. {
  497. if (source == null)
  498. throw new ArgumentNullException(nameof(source));
  499. if (selector == null)
  500. throw new ArgumentNullException(nameof(selector));
  501. return SumCore(source, selector, cancellationToken);
  502. }
  503. private static Task<double?> SumCore(IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  504. {
  505. return source.Aggregate((double?)0.0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  506. }
  507. private static Task<double?> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  508. {
  509. return source.Select(selector).Sum(cancellationToken);
  510. }
  511. private static Task<double?> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<double?>> selector, CancellationToken cancellationToken)
  512. {
  513. return source.Select(selector).Sum(cancellationToken);
  514. }
  515. public static Task<decimal?> Sum(this IAsyncEnumerable<decimal?> source)
  516. {
  517. if (source == null)
  518. throw new ArgumentNullException(nameof(source));
  519. return SumCore(source, CancellationToken.None);
  520. }
  521. public static Task<decimal?> Sum(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  522. {
  523. if (source == null)
  524. throw new ArgumentNullException(nameof(source));
  525. return SumCore(source, cancellationToken);
  526. }
  527. public static Task<decimal?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector)
  528. {
  529. if (source == null)
  530. throw new ArgumentNullException(nameof(source));
  531. if (selector == null)
  532. throw new ArgumentNullException(nameof(selector));
  533. return SumCore(source, selector, CancellationToken.None);
  534. }
  535. public static Task<decimal?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  536. {
  537. if (source == null)
  538. throw new ArgumentNullException(nameof(source));
  539. if (selector == null)
  540. throw new ArgumentNullException(nameof(selector));
  541. return SumCore(source, selector, cancellationToken);
  542. }
  543. public static Task<decimal?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<decimal?>> selector)
  544. {
  545. if (source == null)
  546. throw new ArgumentNullException(nameof(source));
  547. if (selector == null)
  548. throw new ArgumentNullException(nameof(selector));
  549. return SumCore(source, selector, CancellationToken.None);
  550. }
  551. public static Task<decimal?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<decimal?>> selector, CancellationToken cancellationToken)
  552. {
  553. if (source == null)
  554. throw new ArgumentNullException(nameof(source));
  555. if (selector == null)
  556. throw new ArgumentNullException(nameof(selector));
  557. return SumCore(source, selector, cancellationToken);
  558. }
  559. private static Task<decimal?> SumCore(IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  560. {
  561. return source.Aggregate((decimal?)0m, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  562. }
  563. private static Task<decimal?> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  564. {
  565. return source.Select(selector).Sum(cancellationToken);
  566. }
  567. private static Task<decimal?> SumCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<decimal?>> selector, CancellationToken cancellationToken)
  568. {
  569. return source.Select(selector).Sum(cancellationToken);
  570. }
  571. }
  572. }