c# - 使用 Linq 查询设置值?

标签 c# .net linq

在我的应用程序中,我有一个项目列表,我需要按价格排序并为每个项目设置排名/位置索引。我需要存储排名,因为之后价格可能会发生变化。目前我是这样做的:

var sortedlistKFZ = from res in listKFZ orderby res.Price select res;
if (sortedlistKFZ.Any())
{
     int rankPosition = 1;
     foreach (Result kfz in sortedlistKFZ)
     {
           kfz.MesaAdvertNumber = rankPosition;
           rankPosition++;
     }
}

有没有更短的方法呢?

最佳答案

这行得通吗?

int rankPosition = 1;
var sortedListKFZ = listKFZ.OrderBy(r => r.Price).Select(r => {
    r.MesaAdvertNumber = ++rankPosition;
    return r;
});

关于c# - 使用 Linq 查询设置值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1771168/

相关文章:

c# - IEnumerable<T> 的 String.Join(string, string[]) 的模拟

c# - 最大函数 Linq 查询 - Nullable Int 不是 IEnumerable?

c# - 无法从xamarin android调用firebase云函数

C# 文件移动和覆盖

c# - 委托(delegate)给属性(property)

.net - Type和TypeInfo有什么区别或关系?

c# - 复制 .svn 文件?

c# - Excel进程没有关闭

c# - 对于 64 位架构上的算术,.NET 是否将 Int32 提升为 Int64?

c# - Entity Framework 查询的奇怪行为