L2S:将Sql查询转换为Linq

时间:2012-07-31 11:56:31

标签: c# asp.net linq linq-to-sql

我正在尝试将此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 个答案:

答案 0 :(得分:1)

您可以下载Linqer的试用版。这将帮助您将SQL查询转换为Linq查询,在此过程中您可能会学习Linq。

答案 1 :(得分: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)

如果您可以使用额外日期列...如果没有,请在之后再做一次选择。