Sort by multiple fields with LINQ

A few days ago I had a small issue concerning sorting. I use LINQ to SQL to get all products from a DB table and sort them by two fields – IsFavorite (boolean) and AddedOn (datetime). Normally, one will write the following LINQ query:

However, some people prefer using the C# method-based syntax, which lets you do everything .as the SQL-based one. There is a method, called OrderByDescending() which seems to do exactly what we did in the example above.

Having that, the results I got were sorted only by the AddedOn column. I checked my code 10 times and I couldn’t find what the problem was. FInally, I found my solution – ThenByDescending() method. When you want to sort on multiple columns, the first method you should use is OrderBy (OrderByDescending) and then use ThenBy (ThenByDescending) for all other columns.