Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to use Linq group by and sum values from 3 tables

Posted on 2015-01-22
5
Medium Priority
?
247 Views
Last Modified: 2016-02-15
I am know how I'd do this in SQL but am new to Linq and can't figure this one out.

In the database, one Request will have multiple RequestSteps, each RequestStep will have 1 procedure which has the TechHrs attribute
I want to group by Request and Sum up the TechHrs for all the steps
Requests.JPG
If I do this:
var query1 =
	from r in Requests
	from proc in Procedures
	from rs in RequestSteps
	group r by r.RequestNum into rsg
	select new {rsg.First().RequestNum, SUM = rsg.Sum(x => x.TechHrs)};

Open in new window


The x.TechHrs isn't defined

I would think this would work
	select new {rsg.First().RequestNum, SUM = rsg.Sum(x => x.rs.proc.TechHrs) };

Open in new window

But rs isn't defined
0
Comment
Question by:Lab Guy
[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
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40564779
Hi Lab Guy;

The issue with this part of the code,

SUM = rsg.Sum(x => x.rs.proc.TechHrs)

is that x represents a data record of Request and TechHrs is not a member of Request. In your model there should be a field which is a collection of RequestStep which needs to be inserted between x and TechHrs. The code snippet below should do what you need. You will need to replace the refToRequestStep with the field name in Request table that is the collection of RequestStep not shown in your model screen shot.

var query1 =
          from r in Requests
          group r by r.RequestNum into rsg
          from req in rsg
          select new
          {
            RequestNum = rsg.First().RequestNum,
            SUM = rsg.refToRequestStep.Sum(x => x.TechHrs)
          };

Open in new window

0
 

Author Comment

by:Lab Guy
ID: 40565030
If I change my select to this
select rsg;

Open in new window

I get this (using LinqPad)
Results from search in LinqPadSo, looks to me like this should work
	select new {
		rsg.First().RequestNum, 
		SUM = rsg.RequestSteps.Procedures.Sum(x => x.TechHrs) 
	};

Open in new window

But get this error:

'System.Linq.IGrouping<string,LINQPad.User.Request>' does not contain a definition for 'RequestSteps' and no extension method 'RequestSteps' accepting a first argument of type 'System.Linq.IGrouping<string,LINQPad.User.Request>' could be found (press F4 to add a using directive or assembly reference)
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40565271
Sorry I did not realize that the TechHrs field was in the Procedures table. Please try it like this to see if it gives the results you need.

var query1 =
          from r in Requests
          group r by r.RequestNum into rsg
          select new
          {
            RequestNum = rsg.First().RequestNum,
            SUM = (from rr in RequestSteps
                   select rr.RequestSteps.Select( rs => rs.Procedures.TechHrs ).Sum( h => h ))
             )
          }; 

Open in new window

0
 

Author Comment

by:Lab Guy
ID: 40565382
Ok, getting closer, still not recognizing RequestSteps
Linq Results
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40565469
Try this and see if it works. If it is not working with this change can you upload the database to the web so I can test with it if is small.

Remove this line from the query.

from req in rsg

And change the last line of the query to this.

TechHrsSUM = (from rr in rsg
              select rr.RequestSteps.Select( rs => rs.Procedures.TechHrs ).Sum( h => h ))
             )

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
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

609 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