Solved

DataSet Sum And Subtract 2 Columns?

Posted on 2011-02-28
7
1,003 Views
Last Modified: 2013-11-11
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();
0
Comment
Question by:pointeman
  • 3
  • 2
  • 2
7 Comments
 
LVL 16

Expert Comment

by:sjklein42
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

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

Expert Comment

by:sjklein42
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" 
	select (SUM(dr.Field<int>("QtySold")) - SUM(dr.Field<int>("QtyBuy")));

Open in new window

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:pointeman
ID: 34999432
Don't have "Sum" in LINQ.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 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( );  

Open in new window


Fernando
0
 

Author Closing Comment

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

Thank You...
0
 
LVL 62

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now