pointeman
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();
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"
&& SUM(dr.Field<int>("QtySold
select ...;
Label8.Text = "ret= "+ ret.ToString();
I think the problem may be that you've put the SUM...-SUM... expression in your "where" clause rather than the "select" part.
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")));
ASKER
Don't have "Sum" in LINQ.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The missing code: "Group t By ..."
Thank You...
Thank You...
Not a problem, glad I was able to help. ;=)