如何管理多个OrderByDescending标准?

Omi*_*-RH 11 c# linq

我想获得一个按优先级排序的三个属性的列表

  1. 至今
  2. RunDate

我的代码在这里

MyList
    .OrderByDescending(p => p.ToDate)
    .OrderByDescending(p => p.Number)
    .OrderByDescending(p => p.RunDate)
    .FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)

但结果不正确.

例如,当MyList包含两个元素:e1,e2和e1.ToDate> e2.ToDate时,结果为e2.

哪个属性应该先来?具有最高优先级(ToDate)或最低优先级(RunDate)的属性?

Jon*_*eet 25

我怀疑你真的想要:

MyList
.OrderByDescending(p => p.ToDate)
.ThenByDescending(p => p.Number)
.ThenByDescending(p => p.RunDate)
.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)

ThenBy并且ThenByDescending在您使用OrderBy或提供主要订单后用于指定次要订单OrderByDescending.