2012-07-31 128 views
1

我想這個SQL查詢LINQ轉換,但未能如願.. 任何幫助表示讚賞...L2S:轉換SQL查詢到LINQ

SELECT [t0].id, 
    sum([t1].[total_Statements]) AS [value], 
    sum([t1].[totalpage] - [t1].[total_Statements]) AS [value2], 
    sum([t1].[total_bad]) AS [value3], 
    [t0].[biller_id] 
    FROM [dbo].[upload_t] AS [t0] 
    left JOIN [dbo].[printer] AS [t1] ON 
    ((CONVERT(NVarChar,[t0].[id])) = [t1].bill]) 
    where (([t0].[biller_id] = 10) 
    group by [t0].[biller_id],[t0].id,[t0].[date] 
    ORDER BY [t0].[date] DESC 
+2

[你有什麼嘗試](http://whathaveyoutried.com/)? – 2012-07-31 12:01:08

+1

你失敗了? – leppie 2012-07-31 12:02:41

+0

Darek得到了解決方案..感謝 – 2012-07-31 12:15:11

回答

1
var query = 
ctx.upload_t.Where(w1=>w1.biller_id==10) 
.join(ctx.printer, t0=>t0.id.ToString(), t1=>t1.bill.ToString(), 
(t0,t1) => new {t0.id, t0.biller_id, t1.total_statements, t1.total_page, t1.total_bad,t0.date}) 
.GroupBy(g1=>new {t0.biller_id,t0.id,t0.date) 
.Select(s1=>new{ 
    s1.Key.id, 
    value=s1.Sum(s2=>s2.total_statements), 
    value2=s1.Sum(s2=>s2.total_page-s2.total_statements), 
    value3=s1.Sum(s2=>s2.total_bad), 
    s1.Key.biller_id, s1.Key.date}) 
.OrderBy(o1=>o1.date) 

如果你可以住在額外的日期列......如果沒有,做後續選擇。

+0

感謝您快速回答Darek .. – 2012-07-31 12:15:28

1

你可以下載一個試用版Linqer。這將幫助你將SQL查詢轉換爲Linq查詢,並且在這個過程中你可以學習Linq。

+0

這不是一個解決方案。它是另一種選擇。應該是一個評論。 – 2012-07-31 12:10:07

+0

是的。這就是爲什麼我說你的答案應該是一個評論。 – 2012-07-31 12:18:25