Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sum column on datatable with 2 different results

i have a datatable with a Column: Total
this is the datatable:

Totale (datatype: decimal)
2,9 
2,9

i need to sum of this comumns, then i tried this code:

 Dim Totale As Double = dt.AsEnumerable().Sum(Function(r) IIf(IsDBNull(r("Totale")), 0, r("Totale")))
Dim Totale As Decimal= dt.AsEnumerable().Where(Function(r) IsDBNull(r("Totale")) = False).Sum(Function(r) r("Totale"))
  Dim Totale As Decimal= Convert.ToDecimal(dt.Compute("SUM(Totale)", "Totale is not null"))

This gives me the result of:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

6
6
5,8

wich only the last one is correct! my question is WHY the first 2 sum give me 6 as result
thanks

>Solution :

Because you aren’t casting the r("Totale"), so the iterator sums withouth precission, Try:

Dim Totale As Decimal= dt.AsEnumerable().Sum(Function(r) IIf(IsDBNull(r("Totale")), 0, Convert.ToDecimal(r("Totale"))))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading