3
  var GrandTotal = dt.AsEnumerable().Sum(cols => Convert.ToDecimal(cols.Field<string>("TotalPrice"))); 

is Giving Error:

Unable to cast object of type 'System.Decimal' to type 'System.String'

How can I Correct it?

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
SRJ
  • 198
  • 1
  • 4
  • 22

1 Answers1

3

Your TotalPrice column contains decimal values, but you are trying to cast row field value to string. Cast to decimal directly instead:

var GrandTotal = dt.AsEnumerable()
                   .Sum(r => r.Field<decimal>("TotalPrice")); 
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459