Avatar of NickMalloy
NickMalloy
Flag for United States of America asked on

Getting the Sum of a database column.

I have a database column that I am trying to get the sum for every record. In my test sample I am only returning 6 records

My column is called WageAmount. I have tried everything and my decimal shows a value of 0. The statement returns no row with a null value.

convert(decimal(18,2),Sum(PR.WAGE_AMOUNT end)) AS [WageAmount],

In c# my code is
 decimal d = 0;
                decimal total = dt.AsEnumerable().Where(r => !r.IsNull("WageAmount") && decimal.TryParse(r["WageAmount"].ToString(), out d)).Sum(r => d);

d = 0 and total = 0. What am I doing wrong?
.NET ProgrammingDatabasesC#

Avatar of undefined
Last Comment
Shane Kahkola

8/22/2022 - Mon
Dorababu M

How about this way, code here https://dotnetfiddle.net/xfvhv8

DataTable dt = new DataTable();
dt.Columns.Add("Id",typeof(int));
dt.Columns.Add("Name",typeof(string));
dt.Columns.Add("Amount",typeof(decimal));
dt.Rows.Add(1,"A",10);
dt.Rows.Add(2,"b",20);
dt.Rows.Add(3,"c",30);
object sumObject;
sumObject = dt.Compute("Sum(Amount)", string.Empty);
Console.WriteLine(sumObject);

Open in new window

ASKER CERTIFIED SOLUTION
Dorababu M

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Shane Kahkola

Can you post a sanitized version of your SQL query? I assume you are collecting the data from the database. The best way to accomplish what you want is to do it in the query, and not after the fact--though after the fact can work.
Your help has saved me hundreds of hours of internet surfing.
fblack61