LINQ OrderBy Operator (Ascending)

  Total Views : 328
  Comments : 0
 
  Keywords : How to sort list of values in ascending order using linq orderby operator example, LINQ orderby operator to sort collection values in ascending order with example, Sort list in asc in linq

Here we will learn linq orderby sorting operator with example in c#, vb.net and how to use linq orderby operator to sort list of values in ascending order with example in c#, vb.net.

LINQ OrderBy Sorting Operator

In linq orderby operator is used to sort list / collection values in ascending order. In linq if we use orderby operator by default it will sort list of values in ascending order we don't need to add any ascending condition in 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.

 

C# Code

 

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

VB.NET Code

 

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 orderby operator in linq to sort list / collection values in ascending order by default.

 

C# Code

 

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; }

}

}

 

VB.NET Code

 

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 above example here we declared a variable studentname of type var and used order by clause in the student collection and mentioned column name "Name" to sort list of values in ascending order based on "Name".

Output of LINQ OrderBy Operator Example

Following is the result of using 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.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User