In this tutorial, we will learn about sorting operator - ThenBy & ThenByDescending in LINQ
In LINQ, sorting operators are used to sort the data from the collection in a specific order such as in ascending or descending order. In this tutorial, we will learn about the sorting operator - ThenBy & ThenByDescending.
The ThenBy
& ThenByDescending
both are extension methods used for sorting collection in ascending or descending order on multiple fields.
The ThenBy
method is used to sort the collection in ascending order whereas the ThenByDescending
method is used to sort the collection in descending order.
The ThenBy
or ThenByDescending
method can be used more than once in the same LINQ query.
The OrderBy()
extension method is used for sorting the collection in an ascending order on a specified field. If you want to sort the collection on secondary fields also, use the ThenBy()
extension method after the OrderBy()
extension method which sorts the collection in ascending order based on another field.
If you want to sort the collection in descending order on secondary fields, use the ThenByDescending()
extension method after the OrderBy()
or OrderByDescending()
extension method which sorts the collection in descending order based on a secondary field.
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqPrograms
{
internal class Program
{
public static void Main(string[] args)
{
// Employee collection
IList<Employee> employeelist = new List<Employee>() {
new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000} ,
new Employee() { EmpID = 2, EmpName = "Moin", City = "New Orleans", Salary = 210000 } ,
new Employee() { EmpID = 3, EmpName = "Bill", City = "Seattle", Salary = 180000 } ,
new Employee() { EmpID = 4, EmpName = "Ram" , City = "Delhi", Salary = 200000} ,
new Employee() { EmpID = 5, EmpName = "Ron" , City = "San Jose", Salary = 150000 },
new Employee() { EmpID = 6, EmpName = "Bill" , City = "Chicago", Salary = 250000 }
};
// LINQ Method Syntax to sort an Employee
// in a ascending Order based on multiple
// fields like EmpName, Salary
Console.WriteLine("--Employee Data Sorted on Multiple Field in Ascending Order--");
var ThenByResult = employeelist
.OrderBy(x => x.EmpName)
.ThenBy(y => y.Salary);
foreach (var emp in ThenByResult)
{
Console.WriteLine($"EmpId = {emp.EmpID}; EmpName = {emp.EmpName}; City = {emp.City}; Salary = {emp.Salary}");
}
Console.WriteLine();
// LINQ Method Syntax to sort an Employee
// first in an ascending order based on EmpName, then,
// in a Descending Order based on secondary
// fields like Salary
Console.WriteLine("--Employee Data Sorted on Multiple Field First in Ascending, then Descending Order--");
var orderByDescendingResult = employeelist
.OrderBy(x => x.EmpName)
.ThenByDescending(y => y.Salary);
foreach (var emp in orderByDescendingResult)
{
Console.WriteLine($"EmpId = {emp.EmpID}; EmpName = {emp.EmpName}; City = {emp.City}; Salary = {emp.Salary}");
}
Console.ReadKey();
}
}
public class Employee
{
public int EmpID { get; set; }
public string EmpName { get; set; }
public string City { get; set; }
public int Salary { get; set; }
}
}
--Employee Data Sorted on Multiple Field in Ascending Order--
EmpId = 3; EmpName = Bill; City = Seattle; Salary = 180000
EmpId = 6; EmpName = Bill; City = Chicago; Salary = 250000
EmpId = 1; EmpName = John; City = New York; Salary = 130000
EmpId = 2; EmpName = Moin; City = New Orleans; Salary = 210000
EmpId = 4; EmpName = Ram; City = Delhi; Salary = 200000
EmpId = 5; EmpName = Ron; City = San Jose; Salary = 150000
--Employee Data Sorted on Multiple Field First in Ascending, then Descending Order--
EmpId = 6; EmpName = Bill; City = Chicago; Salary = 250000
EmpId = 3; EmpName = Bill; City = Seattle; Salary = 180000
EmpId = 1; EmpName = John; City = New York; Salary = 130000
EmpId = 2; EmpName = Moin; City = New Orleans; Salary = 210000
EmpId = 4; EmpName = Ram; City = Delhi; Salary = 200000
EmpId = 5; EmpName = Ron; City = San Jose; Salary = 150000
Note: ThenBy
or ThenByDescending
is not applicable with Query syntax.
Learn Sorting Operator - Reverse in the next tutorial.