LINQ query using union and aggregation functions

Urh ... my LINQ suddenly turned for the worse !!

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

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

The SQL statement will look something 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 them in the controls.

Any hints, tips, pointers will be greatly appreciated.

+3
source share
1 answer

-, , into,

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};  

: LINQ

0

Source: https://habr.com/ru/post/1761685/


All Articles