Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort by multiple fields in LINQ?

how can I do multiple sort in

 return (from p in _db.Pages where p.int_PostStatusId == 2 select p).OrderByDescending(m => m.int_SortOrder);

i want to do order by by int_PageId as well? first by int_SortOrder then by int_PageId

like image 339
maztt Avatar asked Dec 09 '22 11:12

maztt


1 Answers

Use either ThenBy or ThenByDescending to order the result of an OrderBy or OrderByDescending:

return (...)
    .OrderByDescending(m => m.int_SortOrder)
    .ThenBy(m => m.int_PageId);

Or using the query syntax:

orderby p.int_SortOrder descending, p.int_PageId
like image 142
Mark Byers Avatar answered Mar 05 '23 06:03

Mark Byers