如何使用linq编写查询?

时间:2012-03-26 06:12:27

标签: linq select

如何使用linq编写查询?

SELECT
    (SELECT SUM([quantity] * [price_usd])
        FROM [vi].[dbo].[SALES] 
        WHERE [type_id] =1 and [date_id] in(200701,200702,200703,200704,200705,200706,200707,200708,200709,200710,200711,200712)) as y2007,
    (SELECT SUM([quantity] * [price_usd])
        FROM [vi].[dbo].[SALES] 
        WHERE [type_id] =1 and [date_id] in(200801,200802,200803,200804,200805,200806,200807,200808,200809,200810,200811,200812)) as y2008,
        (SELECT SUM([quantity] * [price_usd])
        FROM [vi].[dbo].[SALES] 
        WHERE [type_id] =1 and [date_id] in(200901,200902,200903,200904,200905,200906,200907,200908,200909,200910,200911,200912)) as y2009

1 个答案:

答案 0 :(得分:1)

那种查询怎么样:

From s in Sales
where s.type_id == 1
group s by s.date_id.ToString().Substring(0,4) into g
select New { Year = g.Key, Sum = g.Sum(i => i.quantity * i.price_usd) }

我没有机会在iPad上试用它,但这个想法很明确,它应该可行。