Linq ThenBy and ThenByDescending Method in C#

The LINQ (Language-Integrated Query) ThenBy and ThenByDescending methods in C# are used for multi-level sorting of data. They are often used in conjunction with the OrderBy and OrderByDescending methods to create a more complex sorting order. In this lesson, we’ll explore the ThenBy and ThenByDescending methods in detail, discussing their syntax, usage, and how they work together to create a multi-level sorting.

Syntax:

The ThenBy and ThenByDescending methods in LINQ have a similar syntax:

public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey>(
    this IOrderedEnumerable<TSource> source,
    Func<TSource, TKey> keySelector
);

public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(
    this IOrderedEnumerable<TSource> source,
    Func<TSource, TKey> keySelector
);
Both methods take two parameters:
  • The source parameter, which is an IOrderedEnumerable<TSource> type, representing the ordered sequence of elements to sort.
  • The keySelector parameter, which is a function that selects the key to use for sorting.

How it works:

The ThenBy and ThenByDescending methods in LINQ work by applying a secondary sort order to an already sorted sequence of elements. The primary sort order is established using the OrderBy or OrderByDescending method, and the secondary sort order is established using the ThenBy or ThenByDescending method.

For example, consider the following list of employees:
List<Employee> employees = new List<Employee> 
{
    new Employee { Name = "John", Age = 30, Department = "Marketing" },
    new Employee { Name = "Jane", Age = 25, Department = "Sales" },
    new Employee { Name = "Bob", Age = 35, Department = "Marketing" },
    new Employee { Name = "Sally", Age = 28, Department = "Sales" }
};

If we want to sort this list first by department and then by age, we can use the following LINQ query:

var sortedEmployees = employees.OrderBy(e => e.Department)
                                .ThenBy(e => e.Age);

In this example, we first sort the list by department using the OrderBy method, and then we apply a secondary sort order by age using the ThenBy method. The resulting sequence will be:

Bob, 35, Marketing
John, 30, Marketing
Jane, 25, Sales
Sally, 28, Sales

Applications:

The ThenBy and ThenByDescending methods in LINQ are useful for sorting data in a variety of real-world programming applications. They can be used to sort data in a database query, to sort lists of objects, or to sort collections of data in memory. They are especially useful when sorting large data sets or when sorting data in complex ways.

Conclusion:

The LINQ ThenBy and ThenByDescending methods in C# are essential tools for multi-level sorting of data. Understanding how these methods work and how to use them can help you write more efficient and effective code. Whether you’re a beginner or an experienced programmer, mastering the use of the ThenBy and ThenByDescending methods can help you create more powerful and flexible sorting algorithms.