Link to home
Start Free TrialLog in
Avatar of pointeman
pointemanFlag for United States of America

asked on

DataSet Sum And Subtract 2 Columns?

I have a DataTable with 4 columns (ID, Product, QtySold, QtyBuy).
I would like to return Sum(QtySold) - Sum(QtyBuy). So far not working...

int ret = (from DataRow dr in dt.AsEnumerable()
                       where dr.Field<string>("Product") == "BEER"                      
                       && SUM(dr.Field<int>("QtySold")) - SUM(dr.Field<int>("QtyBuy"))
                       select ...;
       
        Label8.Text = "ret= "+ ret.ToString();
Avatar of sjklein42
sjklein42
Flag of United States of America image

I think the problem may be that you've put the SUM...-SUM... expression in your "where" clause rather than the "select" part.

Avatar of pointeman

ASKER

Agreed, I don't know how to complete the code.
I think it may be as simple as this?

int ret = (from DataRow dr in dt.AsEnumerable() 
	where dr.Field<string>("Product") == "BEER" 
	select (SUM(dr.Field<int>("QtySold")) - SUM(dr.Field<int>("QtyBuy")));

Open in new window

Don't have "Sum" in LINQ.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The missing code: "Group t By ..."

Thank You...
Not a problem, glad I was able to help.  ;=)