2012-05-30 41 views
0

我有一個SQL查詢,我用它來獲得測量分佈到多個垃圾箱的範圍分組:LINQ查詢來獲取基於計算值

SELECT FLOOR(Value/@Step) * @Step AS Bin,   
     COUNT(*) AS Cnt FROM Measurements WHERE (StepId = @StepId) 
     GROUP BY Bin ORDER BY Bin 

其中 值=測量返回值基於StepId (測量表中的主鍵) Step =實際上組數(分配中的倉位)。如何使用LINQ並根據動態創建的值範圍創建分組。

請指教。

回答

0

我覺得這是你追求的:

double step = 100; 
var stepId = 1; 

from m in context.Measurements 
where m.StepId == stepId; 
let bin = (Math.Floor(c.Value/step)) * step 
orderby bin 
group m by bin into x 
select new { x.Key, Count = x.Count() } 

你甚至可以省略樓的功能,因爲在整數「/」經營者在SQL相同的功能。