문제

  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?

도움이 되었습니까?

해결책

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")); 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top