I want to get a list that order by three property that by priority is
My code is here
MyList
.OrderByDescending(p => p.ToDate)
.OrderByDescending(p => p.Number)
.OrderByDescending(p => p.RunDate)
.FirstOrDefault();
But the result is incorrect.
For example when MyList
contains two elements: e1, e2 and e1.ToDate > e2.ToDate, the result is e2.
Which property should come first? The property with highest priority (ToDate) or lowest one (RunDate)?
I suspect you really want:
MyList
.OrderByDescending(p => p.ToDate)
.ThenByDescending(p => p.Number)
.ThenByDescending(p => p.RunDate)
.FirstOrDefault();
ThenBy
and ThenByDescending
are used to specify secondary orderings after you've provided a primary one using OrderBy
or OrderByDescending
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With