LINQ ThenByDescending Sorting Operator

In LINQ, the ThenByDescending sorting operator is used to implement sorting on multiple fields in the list/collection. By default, the ThenByDescending operator will sort list items in descending order, and in LINQ, we use the ThenByDescending operator along with the OrderBy operator.

 

In LINQ ThenByDescending operator is used to specify the second sorting condition to be descending, and the OrderBy operator is used to specify the primary sorting condition. 

Syntax of LINQ ThenByDescending Operator

Following is the syntax of using the LINQ ThenByDescending operator to implement sorting on list/collection items along with the OrderBy operator.

 

C# Code

 

var studentname = Objstudent.OrderBy(x => x.Name).ThenByDescending(x => x.RoleId);

VB.NET Code

 

Dim studentname = Objstudent.OrderBy(Function(x) x.Name).ThenByDescending(Function(x) x.RoleId)

If you observe the above syntax, we defined the first sorting condition with the OrderBy operator and the second condition with the ThenByDescending operator. We sorted the list of items using “Name” and added another field, “RoleId” by using the ThenByDescending operator. Let’s see this with the help of an example.

Example of LINQ ThenByDescending Operator

Following is the example of using the LINQ ThenByDescending operator to sort list/collection items based on multiple fields.

 

C# Code

 

using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQExamples
{
   class Program
   {
      static void Main(string[] args)
      {
         List<Student> Objstudent = new List<Student>()
         {
           new Student() { RoleId=1, Name = "Suresh Dasari", Gender = "Male", Subjects = new List<string> { "Mathematics","Physics" } },
           new Student() { RoleId=2, Name = "Rohini Alavala", Gender = "Female", Subjects = new List<string> { "Entomology", "Botany" } },
           new Student() { RoleId=3, Name = "Praveen Kumar", Gender = "Male", Subjects = new List<string> { "Computers","Operating System", "Java" } },
           new Student() { RoleId=4, Name = "Sateesh Chandra", Gender = "Male", Subjects = new List<string> { "English", "Social Studies", "Chemistry" } },
           new Student() { RoleId=5, Name = "Madhav Sai", Gender = "Male", Subjects = new List<string> { "Accounting", "Charted" } }
         };
         var studentname = Objstudent.OrderBy(x => x.Name).ThenByDescending(x => x.RoleId);
         foreach (var student in studentname)
         {
           Console.WriteLine("Name={0} StudentId={1}", student.Name, student.RoleId);
         }
         Console.ReadLine();
      }
   }
   class Student
   {
      public int RoleId { get; set; }
      public string Name { get; set; }
      public string Gender { get; set; }
      public List<string> Subjects { get; set; }
   }
}

VB.NET Code

 

Module Module1

Sub Main()
Dim Objstudent As New List(Of Student)() From {
New Student() With {.RoleId = 1, .Name = "Suresh Dasari", .Gender = "Male", .Subjects = New List(Of String)() From {"Mathematics", "Physics"}},
New Student() With {.RoleId = 2, .Name = "Rohini Alavala", .Gender = "Female", .Subjects = New List(Of String)() From {"Entomology", "Botany"}},
New Student() With {.RoleId = 3, .Name = "Praveen Kumar", .Gender = "Male", .Subjects = New List(Of String)() From {"Computers", "Operating System", "Java"}},
New Student() With {.RoleId = 4, .Name = "Sateesh Chandra", .Gender = "Male", .Subjects = New List(Of String)() From {"English", "Social Studies", "Chemistry"}},
New Student() With {.RoleId = 5, .Name = "Madhav Sai", .Gender = "Male", .Subjects = New List(Of String)() From {"Accounting", "Charted"}}
}
Dim studentname = Objstudent.OrderBy(Function(x) x.Name).ThenByDescending(Function(x) x.RoleId)
For Each student In studentname
Console.WriteLine("Name={0} StudentId={1}", student.Name, student.RoleId)
Next
Console.ReadLine()
End Sub

Class Student
Public Property RoleId() As Int32
Get
Return m_RoleId
End Get
Set(ByVal value As Int32)
m_RoleId = value
End Set
EndProperty
Private m_RoleId As Int32
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property Gender() As String
Get
Return m_Gender
End Get
Set(ByVal value As String)
m_Gender = value
End Set
End Property
Private m_Gender As String
Public Property Subjects() As List(Of String)
Get
Return m_Subjects
End Get
Set(ByVal value As List(Of String))
m_Subjects = value
End Set
End Property
Private m_Subjects As List(Of String)
End Class
End Module

If you observe the above example, we are sorting “ObjStudent” list items by using multiple fields Name, RoleId.

Result of LINQ ThenByDescending Operator Example

Following is the result of using the LINQ ThenByDescending sorting operator to sort list items based on multiple fields.

 

Name=Madhav Sai StudentId=5
Name=Praveen Kumar StudentId=3
Name=Rohini Alavala StudentId=2
Name=Sateesh Chandra StudentId=4
Name=Suresh Dasari StudentId=1

We learned how to use the LINQ ThenByDescending operator in method syntax with example. Now we will see how to use the LINQ ThenByDescending operator in query syntax with example.

SQL ThenByDescending Operator with Query Syntax

If we use, ThenByDescending operator in LINQ query syntax, that will be like as shown below.

 

C# Code

 

IOrderedEnumerable<Student> studentname = from x in Objstudent
                                          orderby x.Name, x.RoleId descending
                                          select x;
foreach (var student in studentname)
{
   Console.WriteLine("Name={0} StudentId={1}", student.Name, student.RoleId);
}
Console.ReadLine();

VB.NET Code

 

Dim studentname As IOrderedEnumerable(Of Student) = From x In Objstudent Order By x.Name, x.RoleId Descending
For Each student In studentname
Console.WriteLine("Name={0} StudentId={1}", student.Name, student.RoleId)
Next
Console.ReadLine()

This is how we can use LINQ ThenByDescending sorting operator to sort multiple fields in list/collection with method syntax and query syntax.