Saturday 10 June 2017

Multiple "order by" in LINQ



I have two tables, movies and categories, and I get an ordered list by categoryID first and then by Name.



The movie table has three columns ID, Name and CategoryID.
The category table has two columns ID and Name.




I tried something like the following, but it didn't work.



var movies = _db.Movies.OrderBy( m => { m.CategoryID, m.Name })

Answer



This should work for you:



var movies = _db.Movies.OrderBy(c => c.Category).ThenBy(n => n.Name)


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...