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

x
?
Solved

Sql-to-linq Group By and Sum Problem

Posted on 2010-08-13
1
Medium Priority
?
495 Views
Last Modified: 2013-11-11
How can I make a linq for this query

SELECT     SUM(CASE Positive
                            WHEN 0 THEN HowMuch
                            WHEN 1 THEN HowMuch * (- 1)
                          END)
                     AS Positive
FROM         dbo.Table1 INNER JOIN
                      dbo.Table2 ON dbo.Table1.Table1ID = dbo.Table2.Table1ID INNER JOIN
                      dbo.Table3 ON dbo.Table2.Table2ID = dbo.Table3.Table2ID

var query = from p in db.Table1
                    join q in db.Table2 on p.table1ID equals q.table1ID
                   join r in db.Table3  on q.table2ID equals r.table2ID
                   group r by ?????? into g
                                 select new
                                 {
                                     total = g.Sum(p => p.value)
                                 };
                 
furthermore how can I make the +/- in the sum

right now I can do it in two ways

- by steps, but that is not an elegant way.
I am getting the query group by table3, and at the end I add all of them.

sum = query3.Sum(p=>p.HowMuch); but this one can have the +/-

- Also I can make the query directly.

but I don't like it. I would like to get a nice idea.

Thanks in advance

Diana
0
Comment
Question by:dianar77
[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
1 Comment
 

Accepted Solution

by:
dianar77 earned 0 total points
ID: 33433520
var total1 = from p in db.Table1
                    join q in db.Table2 on p.table1ID equals q.table1ID
                   join r in db.Table3  on q.table2ID equals r.table2ID
                                select new
                                {
                                    ID = 1,
                                    value = (r.Positive)?p.HowMuch:p.HowMuch*(-1)
                                };

                    var total2 = from p in total1
                                 group p by p.ID into g
                                 select new
                                 {
                                     g.Key,
                                     total = g.Sum(p => p.value)
                                 };
0

Featured Post

URL rewriting in AWS CloudFront

A quick how-to guide to implement with a Lambda function!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 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