我想得到一个列表,该列表按优先级为 3 个属性排序
- 截止日期
- 数量
- 运行日期
我的代码在这里
MyList
.OrderByDescending(p => p.ToDate)
.OrderByDescending(p => p.Number)
.OrderByDescending(p => p.RunDate)
.FirstOrDefault();
但结果不正确。
例如,当 MyList
包含两个元素:e1、e2 和 e1.ToDate > e2.ToDate,结果为 e2。
哪个属性应该放在第一位?具有最高优先级 (ToDate) 或最低优先级 (RunDate) 的属性?
最佳答案
我怀疑你真的想要:
MyList
.OrderByDescending(p => p.ToDate)
.ThenByDescending(p => p.Number)
.ThenByDescending(p => p.RunDate)
.FirstOrDefault();
ThenBy
和 ThenByDescending
用于在您使用 OrderBy
提供主要顺序后指定次要顺序或 OrderByDescending
.
关于c# - 如何管理多个 OrderByDescending 条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4658188/