0

我有一个 sql 查询,用于将测量值分布到多个 Bins 中:

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

其中 Value = 基于 StepId(测量表中的主键)返回的测量值 Step = 实际上是组数(分布中的 Bins)。

如何LINQ根据动态创建的值范围使用和创建分组。

请指教。

4

1 回答 1

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() }

您甚至可以省略 Floor 函数,因为整数上的“/”运算符在 SQL 中具有相同的功能。

于 2012-05-31T20:25:48.330 回答