In LINQ, the ThenBy sorting operator is used to implement sorting on multiple fields, and by default, the ThenBy operator will sort collection items in ascending order. Generally, in LINQ ThenBy operator is used along with the OrderBy operator to implement sorting on multiple fields in the list/collection.
In LINQ, if we want more than one sorting condition, we can use the ThenBy clause with the 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 the ThenBy operator in LINQ to implement sorting on multiple fields.
LINQ ThenBy Syntax in C#
var studentname = Objstudent.OrderBy(x => x.Name).ThenBy(x => x.RoleId);
LINQ ThenBy Syntax in VB.NET
Dim studentname = Objstudent.OrderBy(Function(x) x.Name).ThenBy(Function(x) x.RoleId)
If you observe the above syntax first, we are sorting the 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 the LINQ ThenBy operator to sort list/collection items based on multiple fields.
LINQ ThenBy Example in C#
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; }
}
}
LINQ ThenBy Example in VB.NET
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
End Property
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 using multiple fields Name, RoleId.
Result of LINQ ThenBy Operator Example
Following is the result of using the 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 the LINQ ThenBy operator in method syntax with example. Now we will see how to use the 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.