LINQ ThenByDescending Sorting Operator

  Total Views : 240
  Comments : 0
 
  Keywords : Linq thenbydescending operator example, Use linq thenbydescending with orderby operator example, Linq orderby thenby descending with example, Linq thenbydescending to order multiple fields

Here we will learn linq thenbydescending operator with example in c#, vb.net and how to use linq thenbydescending with OrderBy operator to implement sorting on multiple fields in list / collection with example in c#, vb.net and use linq thenby descending with query syntax and method syntax with examples in c#, vb.net.

LINQ ThenByDescending Sorting Operator

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

 

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

Syntax of LINQ ThenByDescending Operator

Following is the syntax of using LINQ ThenByDescending operator to implement sorting on list / collection items along with 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 above syntax we defined first sorting condition with OrderBy operator and second condition with ThenByDescending operator. We are sorting list of items using “Name” and  we added another field “RoleId” by using ThenByDescending operator. Let’s see this with the help of an example.

Example of LINQ ThenByDescending Operator

Following is the example of using 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 { getset; }

public string Name { getset; }

public string Gender { getset; }

public List<string> Subjects { getset; }

}

}

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() AsS tring

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 above example we are sorting “ObjStudent” list items by using multiple fields Name, RoleId.

Output of LINQ ThenByDescending Operator Example

Following is the result of using 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 linq ThenByDescending operator in method syntax with example. Now we will see how to use 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.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User