有没有一种方法可以在Linq中与order by子句一起使用?

时间:2018-12-15 17:24:01

标签: c# sql linq

我需要获取有序值的运行总和,因此我需要在linq中复制此查询

SELECT  sum(number) over (order by number)

FROM tableName

你知道一种方法吗?

1 个答案:

答案 0 :(得分:1)

也许这可行:

已编辑(我已经尝试过查询,但我想这是您要找的东西

List<TableName> tableNames = new List<TableName> {
    new TableName { Number = 2},
    new TableName { Number = 4},
    new TableName { Number = 3},
    new TableName { Number = 3},
    new TableName { Number = 2},
    new TableName { Number = 3},
    new TableName { Number = 4},
    new TableName { Number = 4},
    new TableName { Number = 4},
    new TableName { Number = 4}
};

IEnumerable<int> runningSum = tableNames.Select(r => (tableNames.Where(ri => ri.Number <= r.Number).Sum(rii => rii.Number))).OrderBy(num => num);

结果:

4
4
13
13
13
33
33
33
33
33