LINQ ThenBy Sorting Operator

  Total Views : 288
  Comments : 0
 
  Keywords : How to use linq thenby sorting operator with orderby to sort list / collection items with example, LINQ then by sorting operator example to sort list of values in ascending order

Here we will learn LINQ ThenBy sorting operator with example in c#, vb.net, how to use LINQ ThenBy with OrderBy operator to sort multiple fields in list / collection with example in c#, vb.net and how to define LINQ ThenBy with OrderBy in method syntax and query syntax with examples in c#, vb.net.

LINQ ThenBy Sorting Operator

In LINQ ThenBy sorting operator is used to do implement sorting on multiple fields and by default ThenBy operator will sort collection items in ascending order. Generally, in LINQ ThenBy operator is used along with OrderBy operator to implement sorting on multiple fields in list / collection.

 

In LINQ if we want more than one sorting condition then we use ThenBy clause with OrderBy clause. In LINQ OrderBy is the primary sorting operator and ThenBy is a secondary sorting operator. 

Syntax of LINQ ThenBy Operator

Following is the syntax of using ThenBy operator in LINQ to implement sorting on multiple fields.

 

C# Code

 

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

VB.NET Code

 

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

If you observe above syntax first, we are sorting list of items using “Name” and we added another field “RoleId” by using ThenBy condition to sort list items. Let’s see this with the help of an example.

Example of LINQ ThenBy Operator

Following is the example of using linq ThenBy 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).ThenBy(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).ThenBy(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 using multiple fields Name, RoleId.

Output of LINQ ThenBy Operator Example

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

SQL ThenBy Operator with Query Syntax

If we use, ThenBy sorting 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

                                                                              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

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 ThenBy 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