Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to aggregate decimal with Linq

Tags:

c#

linq

I try to

Collection.Aggregate(0.0,(current, next) => current += next.decimalValue);

but I get error of casting decimal to double

another issue: For "Sum" Linq the seed is the default type value? meaning seed = 0 for decimal...

like image 503
Elad Benda Avatar asked Jan 18 '26 03:01

Elad Benda


1 Answers

The compiler interprets 0.0 as a double, you need to specify the type as a decimal. Try the following:

Collection.Aggregate(0m, (current, next) => current += next.decimalValue);

The m suffix indicates that the numeric real literal (in this case 0) is a decimal.

like image 75
Rich O'Kelly Avatar answered Jan 19 '26 18:01

Rich O'Kelly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!