LINQ OrderBy Operator (Ascending)

In LINQ, the OrderBy operator is used to sort list/collection values in ascending order. In LINQ, if we use orderby operator by default, it will sort a list of values in ascending order; we don't need to add any ascending condition in the query statement.

Syntax of LINQ OrderBy Operator

Following is the syntax of using orderby operator in LINQ to sort list/collection values in ascending order.

LINQ OrderBy Syntax in C#

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

LINQ OrderBy Syntax in VB.NET

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

If you observe above syntax we are sorting "Objstudent" collection values using "OrderBy" sorting operator.

LINQ OrderBy Operator Example

Following is the example of using the orderby operator in LINQ to sort list/collection values in ascending order by default.

LINQ OrderBy Example in C#

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

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

LINQ OrderBy Example in VB.NET

Module Module1

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

Class Student
Public Property Name() As String
Get
Return m_Name
End Get
Set(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(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(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 declared a variable studentname of type var and used order by clause in the student collection and mentioned column name "Name" to sort the list of values in ascending order based on "Name".

Result of LINQ OrderBy Operator Example

Following is the result of using the LINQ orderby operator to sort list/collection data in ascending order.

 

Madhav Sai
Praveen Kumar
Rohini Alavala
Sateesh Chandra
Suresh Dasari

This is how we can use LINQ orderby sorting operator to sort list/collection values in ascending order.