Generally, ThenBy method is used with the OrderBy method. The OrderBy() Method, first sort the elements of the sequence or collection in ascending order after that ThenBy() method is used to again sort the result of OrderBy() method in ascending order.
In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In LINQ, if we use order by the operator by default, it will sort the list of values in ascending order. We don't need to add any ascending condition in the query statement.
For LINQ to Objects, it's a stable quicksort that is used.
MyList.OrderBy(x => x.StartDate).ThenByDescending(x => x.EndDate);
Use ThenByDescending
:
var hold = MyList.OrderBy(x => x.StartDate)
.ThenByDescending(x => x.EndDate)
.ToList();
You can also use query syntax and say:
var hold = (from x in MyList
orderby x.StartDate, x.EndDate descending
select x).ToList();
ThenByDescending
is an extension method on IOrderedEnumerable
which is what is returned by OrderBy
. See also the related method ThenBy
.
If you have two or more field to order try this:
var soterdList = initialList.OrderBy(x => x.Priority).
ThenBy(x => x.ArrivalDate).
ThenBy(x => x.ShipDate);
You can add other fields with clasole "ThenBy"
MyList.OrderBy(x => x.StartDate).ThenByDescending(x => x.EndDate);
Note that you can use as well the Descending keyword in the OrderBy (in case you need). So another possible answer is:
MyList.OrderByDescending(x => x.StartDate).ThenByDescending(x => x.EndDate);
VB.NET
MyList.OrderBy(Function(f) f.StartDate).ThenByDescending(Function(f) f.EndDate)
OR
From l In MyList Order By l.StartDate Ascending, l.EndDate Descending
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