ThenBy is one of the ordering query operators available in linq. You can use ThenBy operator with linq to objects, linq to XML and linq to SQL. You will typically use ThenBy operator to do secondary sort. ThenBy operator is used to sort by more than 1 property. When you want to sort initially you would start with using the orderby operator. Since orderby operator returns IOrderderedEnumerable<T>, you cannot reuse the orderby operator again because it can be only used on sequences that implement IEnumerable<T>. This is where the ThenBy operator comes into play. ThenBy operator is an extension method that is available on input sequence that implement IOrderededEnumerable<T> and also returns IOrderedEnumerable<T> which allows you to reuse it multiple times if you want to sort by more than 1 column or property. The prototype of ThenBy operator looks like this
public static IOrderedEnumerable<T> ThenBy<T, K>(
this IOrderedEnumerable<T> source,
Func<T, K> keySelector)
K : IComparable<K>;
ThenBy operator sorts the input sequence based on key returned by the key selector delegate. The key returned by the keySelector delegate can be of the same type as the input sequence. ThenBy operator enforces that the key returned by the key selector delegate must implement IComparable so it can call the compareto method on the element to determine if two elements are equal to each other. ThenBy forces immediate execution of the query. What this means is the query is no longer differed because in order to determine the sorting, it is essential for ThenBy operator to execute the entire query to determine the correct order of elements in the output sequence. The order of elements returned in the output sequence may not not be the same order as the original sequence if the two elements happens to have the same key element upon which you are sorting. Only order that is guaranteed is the order of elements based on the key element returned by the key selector delegate. Let's look at a an example of using ThenBy operator with linq to objects.
In the above example I am getting all the processes on the system. I then sort the all the processes first by processname and then by virtual memory. In the first part of the example I use the method syntax in which I sort all the process initially using the orderby query operator. The orderby query operator is then followed by ThenBy query operator to do subsequent sort based on virtual memorysize. The same example is also done using the query syntax as well.
Let's look through an example of using ThenBy operator with linq to XML query.
In the above example I use descendants operator to get all the names. I order the names elements first by id attribute and then by the name. From the output below you can see that our output sequence is sorted by id and than by name.
ThenBy query operator can also be used with linq to SQL query. IQueryable provider for linq to SQL converts ThenBy query operator into order by operator which SQL server can understand. Let's look at an example of that.
In the above example I am sorting all the categories by category which has the maximum product and than I sort the categories by the categoryname property. Notice as usual to sort the second time I make use of ThenBy operator to sort the collection.