Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

DataSet Sum And Subtract 2 Columns?

Posted on 2011-02-28
Medium Priority
1,018 Views
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"
select ...;

Label8.Text = "ret= "+ ret.ToString();
0
Question by:pointeman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2
• 2

LVL 16

Expert Comment

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

0

Author Comment

ID: 34998772
Agreed, I don't know how to complete the code.
0

LVL 16

Expert Comment

ID: 34999084
I think it may be as simple as this?

``````int ret = (from DataRow dr in dt.AsEnumerable()
where dr.Field<string>("Product") == "BEER"
``````
0

Author Comment

ID: 34999432
Don't have "Sum" in LINQ.
0

LVL 64

Accepted Solution

Fernando Soto earned 2000 total points
ID: 34999530
Hi pointeman;

If you are looking for the total amount BEER product remains then the query below will do that, the variable total will contain the value.

``````var total = ( from t in dt.AsEnumerable( )
where t.Field<string>( "Product" ) == "BEER"
group t by t.Field<string>( "Product" ) into beerGroup
select beerGroup.Sum( b => b.Field<Int32>( "QtyBuy" ) - b.Field<Int32>( "QtySold" ) )
).FirstOrDefault( );
``````

Fernando
0

Author Closing Comment

ID: 35000547
The missing code: "Group t By ..."

Thank You...
0

LVL 64

Expert Comment

ID: 35000620
Not a problem, glad I was able to help.  ;=)
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with tâ€¦
The object model of .Net can be overwhelming at times â€“ so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applicaâ€¦
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for â€¦
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastrâ€¦
Suggested Courses
Course of the Month5 days, 17 hours left to enroll