Summary: in this tutorial, you’ll learn how to use the LINQ ThenBy() method to sort a sequence in ascending order based on a secondary key.
Introduction to the LINQ ThenBy() method
The LINQ ThenBy() method sorts a sequence in ascending order based on a secondary key after the sequence has been sorted by a primary key using the OrderBy() or OrderByDescending() method:
public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey> (
this IOrderedEnumerable<TSource> source,
Func<TSource, TKey> keySelector
);Code language: C# (cs)In this syntax:
TSourceis the type of elements in thesourcesequence.TKeyis the type of sort key.sourceis the input sequence to sort.keySelectoris a function that selects a key to sort thesourcesequence.
The ThenBy() method returns a new sequence sorted by a key in ascending order with the type of IOrderedEnumerable<TSource>.
If the source or keySelector is null, the method ThenBy() method will throw an ArgumentNullException.
LINQ ThenBy() method example
The following example use the OrderBy() to sort a list of employees by department alphabetically first and then use the ThenBy() method to sort the employees in each department by Salary from low to high:
using static System.Console;
class Employee
{
public string? Name { get; set; }
public string? Department { get; set; }
public int Salary { get; set; }
}
class Program
{
public static void Main()
{
var employees = new List<Employee>()
{
new Employee { Name = "John", Department = "HR", Salary = 50000 },
new Employee { Name = "Jane", Department = "IT", Salary = 60000 },
new Employee { Name = "Bob", Department = "HR", Salary = 45000 },
new Employee { Name = "Sara", Department = "IT", Salary = 55000 },
new Employee { Name = "Tom", Department = "IT", Salary = 65000 }
};
var results = employees.OrderBy(e => e.Department).ThenBy(e => e.Salary);
foreach (var e in results)
{
WriteLine($"{e.Department} - {e.Name} - {e.Salary:C}");
}
}
}Code language: C# (cs)Output:
HR - Bob - $45,000.00
HR - John - $50,000.00
IT - Sara - $55,000.00
IT - Jane - $60,000.00
IT - Tom - $65,000.00Code language: plaintext (plaintext)How it works.
First, define the Employee class that has three properties: Name, Department, and Salary:
class Employee
{
public string? Name { get; set; }
public string? Department { get; set; }
public int Salary { get; set; }
}Code language: C# (cs)Second, create a new list that has five Employee objects:
var employees = new List<Employee>()
{
new Employee { Name = "John", Department = "HR", Salary = 50000 },
new Employee { Name = "Jane", Department = "IT", Salary = 60000 },
new Employee { Name = "Bob", Department = "HR", Salary = 45000 },
new Employee { Name = "Sara", Department = "IT", Salary = 55000 },
new Employee { Name = "Tom", Department = "IT", Salary = 65000 }
};Code language: C# (cs)Third, sort the Employee objects by departments in ascending order using the OrderBy() method and then sort the Employee objects in each department by salary using the ThenBy() method:
var results = employees.OrderBy(e => e.Department)
.ThenBy(e => e.Salary);Code language: C# (cs)Finally, write the employees to the console using a foreach and WriteLine() method:
foreach (var e in results)
{
WriteLine($"{e.Department} - {e.Name} - {e.Salary:C}");
}Code language: C# (cs)Summary
- Use LINQ
ThenBy()method to sort elements in a sequence in ascending order by a secondary key, after the sequence has been sorted by a primary key in ascending order using theOrderBy()method.