• Fabian Wetzel
  • 2012-04-07
  • 1 min. read

Linq Kwik-e: Order by

Imagine you are writing a SQL select statement to read all products from your database ordered by category and then by name, it would look like:

1
2
3
SELECT *
FROM products
ORDER BY category, name

Please note that my textual description follows the notation of the order statements.

Now lets imagine we have a list of products in C# and want to sort them the same way using Linq:

As you can see, it follows the natural description.

I like to directly use the Linq extension methods instead of Linq so I was used to write the same as:

To get the same ordered list as in the samples before, I had to reverse the order of the orderby statements. This feels very unnatural and is easy to get wrong. Luckily I discovered the ThenBy() method:

This way, I can write the statements in the same order as I was used to!

_You may want to read my _earlier Linq Kwik-e_ as well Zwinkerndes Smiley_