LINQ OrderByDescending Operator

  Total Views : 273
  Comments : 0
 
  Keywords : How to sort list of values in descending order using linq orderbydescending operator example, LINQ orderbydescending operator to sort collection values in descending order with example

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

LINQ OrderByDescending Sorting Operator

In LINQ orderbydescending operator is used to sort list / collection of items in descending order.

Syntax of LINQ OrderByDescending Operator

Following is the syntax of using linq orderbydescending operator to sort list of item values in descending order.

 

C# Code

 

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

VB.NET Code

 

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

If you observe above syntax we are sorting Objstudent collection items using linq OrderByDescending operator. 

LINQ OrderByDescending Operator Example

Following is the example of using linq orderbydescending sorting operator to sort list of items in descending order.

 

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() { 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.OrderByDescending(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.OrderByDescending(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(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

 

EndModule

If you observe above example we are using OrderByDescending operator sort list items in descending order.

Output of LINQ OrderByDescending Operator

Following is the result of using LINQ OrderByDescending operator to sort list items in descending order.

 

Suresh Dasari

Sateesh Chandra

Rohini Alavala

Praveen Kumar

Madhav Sai

This is how we can use linq orderbydescending operator to sort list of items in descending order.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User