使用范围和计算值在LINQ中进行分组

时间:2012-05-29 23:50:29

标签: linq grouping range

以下是什么是LINQ等效语句?

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

2 个答案:

答案 0 :(得分:1)

不清楚这些参数或列的来源,但假设LINQ to SQL,这应该或多或少相等:

var step = ...;
var stepId = ...;
var query =
    from m in dc.Measurements
    where m.StepId == stepId
    group m by m.Bin into g
    orderby g.Key
    select new
    {
        Bin = Math.Floor(Value / step) * step, // where did "Value" come from?
        Cnt = g.Count(),
    };

答案 1 :(得分:0)

使用Lambda Expression:

var query = dc.Measurements.AsEnumerable().Where(x=>x.StepId=_stepId).
GroupBy(g=>g.Bin).Select(xg=>new
    {
        Bin = Math.Floor(Value / step) * step, 
        Cnt = xg.Count(),
    }
 );