2010-08-26 120 views
3

Urgh ...我的LINQ突然變壞了!使用連接和聚合函數的LINQ查詢

我有兩個表,並希望加入並在表上執行聚合函數。首先,這是可能的,我假設它是,其次,我如何處理聚合函數?作爲同樣的LINQ聲明的一部分?

的SQL語句看起來有點像這樣:

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) 

顯然,「AS」部分,可以忽略不計,我可以重命名正常對照。

任何提示,技巧,指針將不勝感激。

回答

0

嘗試做一些如下利用與into關鍵詞幫助您加入來實現你的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}; 

檢查這個職位更多詳細信息:LINQ Having Clause and Group By with condition