LINQ Reverse() Method

In LINQ, the Reverse method is used to reverse the list/collection items order.

Syntax of LINQ Reverse Method

Following is the syntax of using the LINQ Reverse method to reverse the list/collection items.

 

C# Code

 

IEnumerable<Student> result = Students.Reverse();

VB.NET Code

 

Dim result As IEnumerable(Of Student) = Students.Reverse()

Let’s look at one example to understand more about the reverse operator.

Example of LINQ Reverse() Method

Following is the example of using the LINQ Reverse() method to reverse the order of the list/collection of items.

 

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" } }
       };
       Console.WriteLine("Iterating Students in the Normal order");
       Console.WriteLine("");
       IEnumerable<Student> Students = Objstudent;
       foreach (var item in Students)
       {
          Console.WriteLine("StudentId={0} Name={1} Gender={2}", item.RoleId, item.Name, item.Gender);
       }
       Console.WriteLine("-------------------------------------------------");
       Console.WriteLine("Iterating Students in the Reverse order");
       Console.WriteLine("");
       IEnumerable<Student> result = Students.Reverse();
       foreach (var item in result)
       {
          Console.WriteLine("StudentId={0} Name={1} Gender={2}", item.RoleId, item.Name, item.Gender);
       }
       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"}}
}
Console.WriteLine("Iterating Students in the Normal order")
Console.WriteLine("")
Dim Students As IEnumerable(Of Student) = Objstudent
For Each item In Students
Console.WriteLine("StudentId={0} Name={1} Gender={2}", item.RoleId, item.Name, item.Gender)
Next
Console.WriteLine("-------------------------------------------------")
Console.WriteLine("Iterating Students in the Reverse order")
Console.WriteLine("")
Dim result As IEnumerable(Of Student) = Students.Reverse()
For Each item In result
Console.WriteLine("StudentId={0} Name={1} Gender={2}", item.RoleId, item.Name, item.Gender)
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 AsList(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 code, we display the "Objstudent" list items without reversing and reversing by using the Reverse() method.

Result of LINQ Reverse() Method Example

Following is the result of the LINQ reverse method example to reverse the list/collection items order.

 

Iterating Students in the Normal order

StudentId=1 Name=Suresh Dasari Gender=Male
StudentId=2 Name=Rohini Alavala Gender=Female
StudentId=3 Name=Praveen Kumar Gender=Male
StudentId=4 Name=Sateesh Chandra Gender=Male
StudentId=5 Name=Madhav Sai Gender=Male
-------------------------------------------------
Iterating Students in the Reverse order

StudentId=5 Name=Madhav Sai Gender=Male
StudentId=4 Name=Sateesh Chandra Gender=Male
StudentId=3 Name=Praveen Kumar Gender=Male
StudentId=2 Name=Rohini Alavala Gender=Female
StudentId=1 Name=Suresh Dasari Gender=Male

This is how we can use LINQ Reverse() method to change/reverse list/collection items order.