How can I sort by multiple fields in LINQ?

how can i do multiple sortings in

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

I want to place an order by int_PageId? first int_SortOrder, then int_PageId

+3
source share
1 answer

Use ThenByeither ThenByDescendingto order the result OrderByor 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
+13
source

Source: https://habr.com/ru/post/1785619/


All Articles