Multiple “order by” in LINQ

2018-12-31 04:01发布

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 two has 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 })

7条回答
旧人旧事旧时光
2楼-- · 2018-12-31 04:46

Add "new":

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

That works on my box. It does return something that can be used to sort. It returns an object with two values.

Similar, but different to sorting by a combined column, as follows.

var movies = _db.Movies.OrderBy( m => (m.CategoryID.ToString() + m.Name))
查看更多
登录 后发表回答