Question

Urgh...my LINQ has suddenly taken a turn for the worse!!

I have two tables and want to join and perform aggregate functions upon the tables.

Firstly, is this possible, I am assuming it is, and secondly, how do I handle the aggregate functions? As part of the same LINQ statement?

The SQL statement will look a bit like this:

SELECT     t1.Col1, t1.Col2, t1.Col3, t2.Col10, 
                      t2.Col11, SUM(t2.Col4) AS TotalPurchases, COUNT(t1.Col5) AS ProductCount,t2.Col7, 
                      t2.Col6, t2.Col8, t2.Col9
FROM         t1 INNER JOIN
                      t2 ON t1.Col1 = t2.Col1 AND t1.Col5 = t2.Col5 AND 
                      t1.Col2 = t2.Col2
GROUP BY t1.Col1, t1.Col2, t1.Col3, t2.Col7, t2.Col6, 
                      t2.Col8, t2.Col9, t2.Col10, t2.Col11
HAVING      (t1.Col1 = @p1) AND (t1.Col2 = @p2) AND (t1.Col3 = @p3)

Obviously, the 'AS' sections can be ignored, I can rename those in the controls.

Any hints, tips, pointers would be greatly appreciated.

Was it helpful?

Solution

Try to do something as below make use of joins with the into keyword help you to achieve your taks

var query = from t1 in prodDtcx.t1
            join t2 in prodDtcx.t2 on t1.ID equals t2.ID
            group t1.col1,t2.col2 by new {col1= t1.col1,col2=  t2.col2,col3 = t1.col2} into g
            where g.col3 >= @variable
            select new { Col1 = g.col1, Col2 = g.col2};  

Check this post for more detail : LINQ Having Clause and Group By with condition

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top