Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply multiple orderby in linq query

Tags:

I want to apply order by on multiple columns some ascending and others are in descending order in LINQ. How can i do that?

like image 750
Fraz Sundal Avatar asked Jan 06 '11 09:01

Fraz Sundal


People also ask

What is OrderBy in Linq?

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.

What is the use of ThenBy in Linq?

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.


1 Answers

var sortedCollection =
    from item in collection
    orderby item.Property1, item.Property2 descending, item.Property3
    select item;
like image 178
Steven Avatar answered Oct 04 '22 19:10

Steven