1
0

Sum.Generated.cs 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  12. /// <summary>
  13. /// Computes the sum of a sequence of <see cref="int" /> values.
  14. /// </summary>
  15. /// <param name="source">A sequence of <see cref="int" /> values to calculate the sum of.</param>
  16. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  17. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  18. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  19. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  20. public static ValueTask<int> SumAsync(this IAsyncEnumerable<int> source, CancellationToken cancellationToken = default)
  21. {
  22. if (source == null)
  23. throw Error.ArgumentNull(nameof(source));
  24. return Core(source, cancellationToken);
  25. static async ValueTask<int> Core(IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  26. {
  27. var sum = 0;
  28. await foreach (int value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  29. {
  30. checked
  31. {
  32. sum += value;
  33. }
  34. }
  35. return sum;
  36. }
  37. }
  38. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  39. /// <summary>
  40. /// Computes the sum of a sequence of <see cref="int" /> values that are obtained by invoking a transform function on each element of the input sequence.
  41. /// </summary>
  42. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  43. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  44. /// <param name="selector">A transform function to apply to each element.</param>
  45. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  46. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  47. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  48. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  49. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  50. public static ValueTask<int> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken = default)
  51. {
  52. if (source == null)
  53. throw Error.ArgumentNull(nameof(source));
  54. if (selector == null)
  55. throw Error.ArgumentNull(nameof(selector));
  56. return Core(source, selector, cancellationToken);
  57. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  58. {
  59. var sum = 0;
  60. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  61. {
  62. var value = selector(item);
  63. checked
  64. {
  65. sum += value;
  66. }
  67. }
  68. return sum;
  69. }
  70. }
  71. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  72. [GenerateAsyncOverload]
  73. private static ValueTask<int> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  74. {
  75. if (source == null)
  76. throw Error.ArgumentNull(nameof(source));
  77. if (selector == null)
  78. throw Error.ArgumentNull(nameof(selector));
  79. return Core(source, selector, cancellationToken);
  80. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int>> selector, CancellationToken cancellationToken)
  81. {
  82. var sum = 0;
  83. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  84. {
  85. var value = await selector(item).ConfigureAwait(false);
  86. checked
  87. {
  88. sum += value;
  89. }
  90. }
  91. return sum;
  92. }
  93. }
  94. #if !NO_DEEP_CANCELLATION
  95. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  96. [GenerateAsyncOverload]
  97. private static ValueTask<int> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  98. {
  99. if (source == null)
  100. throw Error.ArgumentNull(nameof(source));
  101. if (selector == null)
  102. throw Error.ArgumentNull(nameof(selector));
  103. return Core(source, selector, cancellationToken);
  104. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int>> selector, CancellationToken cancellationToken)
  105. {
  106. var sum = 0;
  107. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  108. {
  109. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  110. checked
  111. {
  112. sum += value;
  113. }
  114. }
  115. return sum;
  116. }
  117. }
  118. #endif
  119. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  120. /// <summary>
  121. /// Computes the sum of a sequence of <see cref="long" /> values.
  122. /// </summary>
  123. /// <param name="source">A sequence of <see cref="long" /> values to calculate the sum of.</param>
  124. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  125. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  126. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  127. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  128. public static ValueTask<long> SumAsync(this IAsyncEnumerable<long> source, CancellationToken cancellationToken = default)
  129. {
  130. if (source == null)
  131. throw Error.ArgumentNull(nameof(source));
  132. return Core(source, cancellationToken);
  133. static async ValueTask<long> Core(IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  134. {
  135. var sum = 0L;
  136. await foreach (long value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  137. {
  138. checked
  139. {
  140. sum += value;
  141. }
  142. }
  143. return sum;
  144. }
  145. }
  146. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  147. /// <summary>
  148. /// Computes the sum of a sequence of <see cref="long" /> values that are obtained by invoking a transform function on each element of the input sequence.
  149. /// </summary>
  150. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  151. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  152. /// <param name="selector">A transform function to apply to each element.</param>
  153. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  154. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  155. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  156. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  157. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  158. public static ValueTask<long> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken = default)
  159. {
  160. if (source == null)
  161. throw Error.ArgumentNull(nameof(source));
  162. if (selector == null)
  163. throw Error.ArgumentNull(nameof(selector));
  164. return Core(source, selector, cancellationToken);
  165. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  166. {
  167. var sum = 0L;
  168. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  169. {
  170. var value = selector(item);
  171. checked
  172. {
  173. sum += value;
  174. }
  175. }
  176. return sum;
  177. }
  178. }
  179. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  180. [GenerateAsyncOverload]
  181. private static ValueTask<long> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  182. {
  183. if (source == null)
  184. throw Error.ArgumentNull(nameof(source));
  185. if (selector == null)
  186. throw Error.ArgumentNull(nameof(selector));
  187. return Core(source, selector, cancellationToken);
  188. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long>> selector, CancellationToken cancellationToken)
  189. {
  190. var sum = 0L;
  191. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  192. {
  193. var value = await selector(item).ConfigureAwait(false);
  194. checked
  195. {
  196. sum += value;
  197. }
  198. }
  199. return sum;
  200. }
  201. }
  202. #if !NO_DEEP_CANCELLATION
  203. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  204. [GenerateAsyncOverload]
  205. private static ValueTask<long> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  206. {
  207. if (source == null)
  208. throw Error.ArgumentNull(nameof(source));
  209. if (selector == null)
  210. throw Error.ArgumentNull(nameof(selector));
  211. return Core(source, selector, cancellationToken);
  212. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long>> selector, CancellationToken cancellationToken)
  213. {
  214. var sum = 0L;
  215. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  216. {
  217. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  218. checked
  219. {
  220. sum += value;
  221. }
  222. }
  223. return sum;
  224. }
  225. }
  226. #endif
  227. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  228. /// <summary>
  229. /// Computes the sum of a sequence of <see cref="float" /> values.
  230. /// </summary>
  231. /// <param name="source">A sequence of <see cref="float" /> values to calculate the sum of.</param>
  232. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  233. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  234. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  235. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  236. public static ValueTask<float> SumAsync(this IAsyncEnumerable<float> source, CancellationToken cancellationToken = default)
  237. {
  238. if (source == null)
  239. throw Error.ArgumentNull(nameof(source));
  240. return Core(source, cancellationToken);
  241. static async ValueTask<float> Core(IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  242. {
  243. var sum = 0.0f;
  244. await foreach (float value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  245. {
  246. sum += value;
  247. }
  248. return sum;
  249. }
  250. }
  251. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  252. /// <summary>
  253. /// Computes the sum of a sequence of <see cref="float" /> values that are obtained by invoking a transform function on each element of the input sequence.
  254. /// </summary>
  255. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  256. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  257. /// <param name="selector">A transform function to apply to each element.</param>
  258. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  259. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  260. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  261. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  262. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  263. public static ValueTask<float> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken = default)
  264. {
  265. if (source == null)
  266. throw Error.ArgumentNull(nameof(source));
  267. if (selector == null)
  268. throw Error.ArgumentNull(nameof(selector));
  269. return Core(source, selector, cancellationToken);
  270. static async ValueTask<float> Core(IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  271. {
  272. var sum = 0.0f;
  273. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  274. {
  275. var value = selector(item);
  276. sum += value;
  277. }
  278. return sum;
  279. }
  280. }
  281. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  282. [GenerateAsyncOverload]
  283. private static ValueTask<float> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  284. {
  285. if (source == null)
  286. throw Error.ArgumentNull(nameof(source));
  287. if (selector == null)
  288. throw Error.ArgumentNull(nameof(selector));
  289. return Core(source, selector, cancellationToken);
  290. static async ValueTask<float> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float>> selector, CancellationToken cancellationToken)
  291. {
  292. var sum = 0.0f;
  293. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  294. {
  295. var value = await selector(item).ConfigureAwait(false);
  296. sum += value;
  297. }
  298. return sum;
  299. }
  300. }
  301. #if !NO_DEEP_CANCELLATION
  302. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  303. [GenerateAsyncOverload]
  304. private static ValueTask<float> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  305. {
  306. if (source == null)
  307. throw Error.ArgumentNull(nameof(source));
  308. if (selector == null)
  309. throw Error.ArgumentNull(nameof(selector));
  310. return Core(source, selector, cancellationToken);
  311. static async ValueTask<float> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float>> selector, CancellationToken cancellationToken)
  312. {
  313. var sum = 0.0f;
  314. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  315. {
  316. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  317. sum += value;
  318. }
  319. return sum;
  320. }
  321. }
  322. #endif
  323. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  324. /// <summary>
  325. /// Computes the sum of a sequence of <see cref="double" /> values.
  326. /// </summary>
  327. /// <param name="source">A sequence of <see cref="double" /> values to calculate the sum of.</param>
  328. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  329. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  330. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  331. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  332. public static ValueTask<double> SumAsync(this IAsyncEnumerable<double> source, CancellationToken cancellationToken = default)
  333. {
  334. if (source == null)
  335. throw Error.ArgumentNull(nameof(source));
  336. return Core(source, cancellationToken);
  337. static async ValueTask<double> Core(IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  338. {
  339. var sum = 0.0;
  340. await foreach (double value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  341. {
  342. sum += value;
  343. }
  344. return sum;
  345. }
  346. }
  347. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  348. /// <summary>
  349. /// Computes the sum of a sequence of <see cref="double" /> values that are obtained by invoking a transform function on each element of the input sequence.
  350. /// </summary>
  351. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  352. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  353. /// <param name="selector">A transform function to apply to each element.</param>
  354. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  355. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  356. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  357. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  358. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  359. public static ValueTask<double> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken = default)
  360. {
  361. if (source == null)
  362. throw Error.ArgumentNull(nameof(source));
  363. if (selector == null)
  364. throw Error.ArgumentNull(nameof(selector));
  365. return Core(source, selector, cancellationToken);
  366. static async ValueTask<double> Core(IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  367. {
  368. var sum = 0.0;
  369. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  370. {
  371. var value = selector(item);
  372. sum += value;
  373. }
  374. return sum;
  375. }
  376. }
  377. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  378. [GenerateAsyncOverload]
  379. private static ValueTask<double> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  380. {
  381. if (source == null)
  382. throw Error.ArgumentNull(nameof(source));
  383. if (selector == null)
  384. throw Error.ArgumentNull(nameof(selector));
  385. return Core(source, selector, cancellationToken);
  386. static async ValueTask<double> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double>> selector, CancellationToken cancellationToken)
  387. {
  388. var sum = 0.0;
  389. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  390. {
  391. var value = await selector(item).ConfigureAwait(false);
  392. sum += value;
  393. }
  394. return sum;
  395. }
  396. }
  397. #if !NO_DEEP_CANCELLATION
  398. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  399. [GenerateAsyncOverload]
  400. private static ValueTask<double> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  401. {
  402. if (source == null)
  403. throw Error.ArgumentNull(nameof(source));
  404. if (selector == null)
  405. throw Error.ArgumentNull(nameof(selector));
  406. return Core(source, selector, cancellationToken);
  407. static async ValueTask<double> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double>> selector, CancellationToken cancellationToken)
  408. {
  409. var sum = 0.0;
  410. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  411. {
  412. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  413. sum += value;
  414. }
  415. return sum;
  416. }
  417. }
  418. #endif
  419. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  420. /// <summary>
  421. /// Computes the sum of a sequence of <see cref="decimal" /> values.
  422. /// </summary>
  423. /// <param name="source">A sequence of <see cref="decimal" /> values to calculate the sum of.</param>
  424. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  425. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  426. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  427. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  428. public static ValueTask<decimal> SumAsync(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken = default)
  429. {
  430. if (source == null)
  431. throw Error.ArgumentNull(nameof(source));
  432. return Core(source, cancellationToken);
  433. static async ValueTask<decimal> Core(IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  434. {
  435. var sum = 0m;
  436. await foreach (decimal value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  437. {
  438. sum += value;
  439. }
  440. return sum;
  441. }
  442. }
  443. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  444. /// <summary>
  445. /// Computes the sum of a sequence of <see cref="decimal" /> values that are obtained by invoking a transform function on each element of the input sequence.
  446. /// </summary>
  447. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  448. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  449. /// <param name="selector">A transform function to apply to each element.</param>
  450. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  451. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  452. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  453. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  454. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  455. public static ValueTask<decimal> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken = default)
  456. {
  457. if (source == null)
  458. throw Error.ArgumentNull(nameof(source));
  459. if (selector == null)
  460. throw Error.ArgumentNull(nameof(selector));
  461. return Core(source, selector, cancellationToken);
  462. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  463. {
  464. var sum = 0m;
  465. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  466. {
  467. var value = selector(item);
  468. sum += value;
  469. }
  470. return sum;
  471. }
  472. }
  473. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  474. [GenerateAsyncOverload]
  475. private static ValueTask<decimal> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  476. {
  477. if (source == null)
  478. throw Error.ArgumentNull(nameof(source));
  479. if (selector == null)
  480. throw Error.ArgumentNull(nameof(selector));
  481. return Core(source, selector, cancellationToken);
  482. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal>> selector, CancellationToken cancellationToken)
  483. {
  484. var sum = 0m;
  485. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  486. {
  487. var value = await selector(item).ConfigureAwait(false);
  488. sum += value;
  489. }
  490. return sum;
  491. }
  492. }
  493. #if !NO_DEEP_CANCELLATION
  494. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  495. [GenerateAsyncOverload]
  496. private static ValueTask<decimal> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  497. {
  498. if (source == null)
  499. throw Error.ArgumentNull(nameof(source));
  500. if (selector == null)
  501. throw Error.ArgumentNull(nameof(selector));
  502. return Core(source, selector, cancellationToken);
  503. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal>> selector, CancellationToken cancellationToken)
  504. {
  505. var sum = 0m;
  506. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  507. {
  508. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  509. sum += value;
  510. }
  511. return sum;
  512. }
  513. }
  514. #endif
  515. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  516. /// <summary>
  517. /// Computes the sum of a sequence of <see cref="Nullable{Int}" /> values.
  518. /// </summary>
  519. /// <param name="source">A sequence of <see cref="Nullable{Int}" /> values to calculate the sum of.</param>
  520. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  521. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  522. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  523. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  524. public static ValueTask<int?> SumAsync(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken = default)
  525. {
  526. if (source == null)
  527. throw Error.ArgumentNull(nameof(source));
  528. return Core(source, cancellationToken);
  529. static async ValueTask<int?> Core(IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  530. {
  531. var sum = 0;
  532. await foreach (int? value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  533. {
  534. checked
  535. {
  536. sum += value.GetValueOrDefault();
  537. }
  538. }
  539. return sum;
  540. }
  541. }
  542. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  543. /// <summary>
  544. /// Computes the sum of a sequence of <see cref="Nullable{Int}" /> values that are obtained by invoking a transform function on each element of the input sequence.
  545. /// </summary>
  546. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  547. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  548. /// <param name="selector">A transform function to apply to each element.</param>
  549. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  550. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  551. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  552. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  553. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  554. public static ValueTask<int?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken = default)
  555. {
  556. if (source == null)
  557. throw Error.ArgumentNull(nameof(source));
  558. if (selector == null)
  559. throw Error.ArgumentNull(nameof(selector));
  560. return Core(source, selector, cancellationToken);
  561. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  562. {
  563. var sum = 0;
  564. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  565. {
  566. var value = selector(item);
  567. checked
  568. {
  569. sum += value.GetValueOrDefault();
  570. }
  571. }
  572. return sum;
  573. }
  574. }
  575. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  576. [GenerateAsyncOverload]
  577. private static ValueTask<int?> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  578. {
  579. if (source == null)
  580. throw Error.ArgumentNull(nameof(source));
  581. if (selector == null)
  582. throw Error.ArgumentNull(nameof(selector));
  583. return Core(source, selector, cancellationToken);
  584. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int?>> selector, CancellationToken cancellationToken)
  585. {
  586. var sum = 0;
  587. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  588. {
  589. var value = await selector(item).ConfigureAwait(false);
  590. checked
  591. {
  592. sum += value.GetValueOrDefault();
  593. }
  594. }
  595. return sum;
  596. }
  597. }
  598. #if !NO_DEEP_CANCELLATION
  599. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  600. [GenerateAsyncOverload]
  601. private static ValueTask<int?> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  602. {
  603. if (source == null)
  604. throw Error.ArgumentNull(nameof(source));
  605. if (selector == null)
  606. throw Error.ArgumentNull(nameof(selector));
  607. return Core(source, selector, cancellationToken);
  608. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int?>> selector, CancellationToken cancellationToken)
  609. {
  610. var sum = 0;
  611. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  612. {
  613. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  614. checked
  615. {
  616. sum += value.GetValueOrDefault();
  617. }
  618. }
  619. return sum;
  620. }
  621. }
  622. #endif
  623. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  624. /// <summary>
  625. /// Computes the sum of a sequence of <see cref="Nullable{Long}" /> values.
  626. /// </summary>
  627. /// <param name="source">A sequence of <see cref="Nullable{Long}" /> values to calculate the sum of.</param>
  628. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  629. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  630. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  631. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  632. public static ValueTask<long?> SumAsync(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken = default)
  633. {
  634. if (source == null)
  635. throw Error.ArgumentNull(nameof(source));
  636. return Core(source, cancellationToken);
  637. static async ValueTask<long?> Core(IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  638. {
  639. var sum = 0L;
  640. await foreach (long? value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  641. {
  642. checked
  643. {
  644. sum += value.GetValueOrDefault();
  645. }
  646. }
  647. return sum;
  648. }
  649. }
  650. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  651. /// <summary>
  652. /// Computes the sum of a sequence of <see cref="Nullable{Long}" /> values that are obtained by invoking a transform function on each element of the input sequence.
  653. /// </summary>
  654. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  655. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  656. /// <param name="selector">A transform function to apply to each element.</param>
  657. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  658. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  659. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  660. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  661. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  662. public static ValueTask<long?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken = default)
  663. {
  664. if (source == null)
  665. throw Error.ArgumentNull(nameof(source));
  666. if (selector == null)
  667. throw Error.ArgumentNull(nameof(selector));
  668. return Core(source, selector, cancellationToken);
  669. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  670. {
  671. var sum = 0L;
  672. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  673. {
  674. var value = selector(item);
  675. checked
  676. {
  677. sum += value.GetValueOrDefault();
  678. }
  679. }
  680. return sum;
  681. }
  682. }
  683. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  684. [GenerateAsyncOverload]
  685. private static ValueTask<long?> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  686. {
  687. if (source == null)
  688. throw Error.ArgumentNull(nameof(source));
  689. if (selector == null)
  690. throw Error.ArgumentNull(nameof(selector));
  691. return Core(source, selector, cancellationToken);
  692. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long?>> selector, CancellationToken cancellationToken)
  693. {
  694. var sum = 0L;
  695. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  696. {
  697. var value = await selector(item).ConfigureAwait(false);
  698. checked
  699. {
  700. sum += value.GetValueOrDefault();
  701. }
  702. }
  703. return sum;
  704. }
  705. }
  706. #if !NO_DEEP_CANCELLATION
  707. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  708. [GenerateAsyncOverload]
  709. private static ValueTask<long?> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  710. {
  711. if (source == null)
  712. throw Error.ArgumentNull(nameof(source));
  713. if (selector == null)
  714. throw Error.ArgumentNull(nameof(selector));
  715. return Core(source, selector, cancellationToken);
  716. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long?>> selector, CancellationToken cancellationToken)
  717. {
  718. var sum = 0L;
  719. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  720. {
  721. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  722. checked
  723. {
  724. sum += value.GetValueOrDefault();
  725. }
  726. }
  727. return sum;
  728. }
  729. }
  730. #endif
  731. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  732. /// <summary>
  733. /// Computes the sum of a sequence of <see cref="Nullable{Float}" /> values.
  734. /// </summary>
  735. /// <param name="source">A sequence of <see cref="Nullable{Float}" /> values to calculate the sum of.</param>
  736. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  737. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  738. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  739. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  740. public static ValueTask<float?> SumAsync(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken = default)
  741. {
  742. if (source == null)
  743. throw Error.ArgumentNull(nameof(source));
  744. return Core(source, cancellationToken);
  745. static async ValueTask<float?> Core(IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  746. {
  747. var sum = 0.0f;
  748. await foreach (float? value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  749. {
  750. sum += value.GetValueOrDefault();
  751. }
  752. return sum;
  753. }
  754. }
  755. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  756. /// <summary>
  757. /// Computes the sum of a sequence of <see cref="Nullable{Float}" /> values that are obtained by invoking a transform function on each element of the input sequence.
  758. /// </summary>
  759. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  760. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  761. /// <param name="selector">A transform function to apply to each element.</param>
  762. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  763. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  764. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  765. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  766. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  767. public static ValueTask<float?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken = default)
  768. {
  769. if (source == null)
  770. throw Error.ArgumentNull(nameof(source));
  771. if (selector == null)
  772. throw Error.ArgumentNull(nameof(selector));
  773. return Core(source, selector, cancellationToken);
  774. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  775. {
  776. var sum = 0.0f;
  777. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  778. {
  779. var value = selector(item);
  780. sum += value.GetValueOrDefault();
  781. }
  782. return sum;
  783. }
  784. }
  785. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  786. [GenerateAsyncOverload]
  787. private static ValueTask<float?> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  788. {
  789. if (source == null)
  790. throw Error.ArgumentNull(nameof(source));
  791. if (selector == null)
  792. throw Error.ArgumentNull(nameof(selector));
  793. return Core(source, selector, cancellationToken);
  794. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float?>> selector, CancellationToken cancellationToken)
  795. {
  796. var sum = 0.0f;
  797. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  798. {
  799. var value = await selector(item).ConfigureAwait(false);
  800. sum += value.GetValueOrDefault();
  801. }
  802. return sum;
  803. }
  804. }
  805. #if !NO_DEEP_CANCELLATION
  806. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  807. [GenerateAsyncOverload]
  808. private static ValueTask<float?> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  809. {
  810. if (source == null)
  811. throw Error.ArgumentNull(nameof(source));
  812. if (selector == null)
  813. throw Error.ArgumentNull(nameof(selector));
  814. return Core(source, selector, cancellationToken);
  815. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float?>> selector, CancellationToken cancellationToken)
  816. {
  817. var sum = 0.0f;
  818. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  819. {
  820. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  821. sum += value.GetValueOrDefault();
  822. }
  823. return sum;
  824. }
  825. }
  826. #endif
  827. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  828. /// <summary>
  829. /// Computes the sum of a sequence of <see cref="Nullable{Double}" /> values.
  830. /// </summary>
  831. /// <param name="source">A sequence of <see cref="Nullable{Double}" /> values to calculate the sum of.</param>
  832. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  833. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  834. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  835. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  836. public static ValueTask<double?> SumAsync(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken = default)
  837. {
  838. if (source == null)
  839. throw Error.ArgumentNull(nameof(source));
  840. return Core(source, cancellationToken);
  841. static async ValueTask<double?> Core(IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  842. {
  843. var sum = 0.0;
  844. await foreach (double? value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  845. {
  846. sum += value.GetValueOrDefault();
  847. }
  848. return sum;
  849. }
  850. }
  851. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  852. /// <summary>
  853. /// Computes the sum of a sequence of <see cref="Nullable{Double}" /> values that are obtained by invoking a transform function on each element of the input sequence.
  854. /// </summary>
  855. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  856. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  857. /// <param name="selector">A transform function to apply to each element.</param>
  858. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  859. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  860. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  861. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  862. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  863. public static ValueTask<double?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken = default)
  864. {
  865. if (source == null)
  866. throw Error.ArgumentNull(nameof(source));
  867. if (selector == null)
  868. throw Error.ArgumentNull(nameof(selector));
  869. return Core(source, selector, cancellationToken);
  870. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  871. {
  872. var sum = 0.0;
  873. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  874. {
  875. var value = selector(item);
  876. sum += value.GetValueOrDefault();
  877. }
  878. return sum;
  879. }
  880. }
  881. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  882. [GenerateAsyncOverload]
  883. private static ValueTask<double?> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  884. {
  885. if (source == null)
  886. throw Error.ArgumentNull(nameof(source));
  887. if (selector == null)
  888. throw Error.ArgumentNull(nameof(selector));
  889. return Core(source, selector, cancellationToken);
  890. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double?>> selector, CancellationToken cancellationToken)
  891. {
  892. var sum = 0.0;
  893. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  894. {
  895. var value = await selector(item).ConfigureAwait(false);
  896. sum += value.GetValueOrDefault();
  897. }
  898. return sum;
  899. }
  900. }
  901. #if !NO_DEEP_CANCELLATION
  902. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  903. [GenerateAsyncOverload]
  904. private static ValueTask<double?> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  905. {
  906. if (source == null)
  907. throw Error.ArgumentNull(nameof(source));
  908. if (selector == null)
  909. throw Error.ArgumentNull(nameof(selector));
  910. return Core(source, selector, cancellationToken);
  911. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double?>> selector, CancellationToken cancellationToken)
  912. {
  913. var sum = 0.0;
  914. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  915. {
  916. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  917. sum += value.GetValueOrDefault();
  918. }
  919. return sum;
  920. }
  921. }
  922. #endif
  923. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  924. /// <summary>
  925. /// Computes the sum of a sequence of <see cref="Nullable{Decimal}" /> values.
  926. /// </summary>
  927. /// <param name="source">A sequence of <see cref="Nullable{Decimal}" /> values to calculate the sum of.</param>
  928. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  929. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  930. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  931. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  932. public static ValueTask<decimal?> SumAsync(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken = default)
  933. {
  934. if (source == null)
  935. throw Error.ArgumentNull(nameof(source));
  936. return Core(source, cancellationToken);
  937. static async ValueTask<decimal?> Core(IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  938. {
  939. var sum = 0m;
  940. await foreach (decimal? value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  941. {
  942. sum += value.GetValueOrDefault();
  943. }
  944. return sum;
  945. }
  946. }
  947. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  948. /// <summary>
  949. /// Computes the sum of a sequence of <see cref="Nullable{Decimal}" /> values that are obtained by invoking a transform function on each element of the input sequence.
  950. /// </summary>
  951. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  952. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  953. /// <param name="selector">A transform function to apply to each element.</param>
  954. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  955. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  956. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  957. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  958. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  959. public static ValueTask<decimal?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken = default)
  960. {
  961. if (source == null)
  962. throw Error.ArgumentNull(nameof(source));
  963. if (selector == null)
  964. throw Error.ArgumentNull(nameof(selector));
  965. return Core(source, selector, cancellationToken);
  966. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  967. {
  968. var sum = 0m;
  969. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  970. {
  971. var value = selector(item);
  972. sum += value.GetValueOrDefault();
  973. }
  974. return sum;
  975. }
  976. }
  977. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  978. [GenerateAsyncOverload]
  979. private static ValueTask<decimal?> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  980. {
  981. if (source == null)
  982. throw Error.ArgumentNull(nameof(source));
  983. if (selector == null)
  984. throw Error.ArgumentNull(nameof(selector));
  985. return Core(source, selector, cancellationToken);
  986. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal?>> selector, CancellationToken cancellationToken)
  987. {
  988. var sum = 0m;
  989. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  990. {
  991. var value = await selector(item).ConfigureAwait(false);
  992. sum += value.GetValueOrDefault();
  993. }
  994. return sum;
  995. }
  996. }
  997. #if !NO_DEEP_CANCELLATION
  998. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  999. [GenerateAsyncOverload]
  1000. private static ValueTask<decimal?> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  1001. {
  1002. if (source == null)
  1003. throw Error.ArgumentNull(nameof(source));
  1004. if (selector == null)
  1005. throw Error.ArgumentNull(nameof(selector));
  1006. return Core(source, selector, cancellationToken);
  1007. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal?>> selector, CancellationToken cancellationToken)
  1008. {
  1009. var sum = 0m;
  1010. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  1011. {
  1012. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  1013. sum += value.GetValueOrDefault();
  1014. }
  1015. return sum;
  1016. }
  1017. }
  1018. #endif
  1019. }
  1020. }