In this tutorial, we’ll learn LINQ Reverse Method in C# with examples.
Reverse is another sorting method in LINQ but it is slightly different from the OrderBy and ThenBy sorting operator. Reverse method can be used both with OrderBy and ThenBy Operator. Reverse method just prints the list in opposite direction. It doesn’t sort in ascending or descending order, it just reverse the current output. You can understand it using the following programming example.
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | using System; using System.Linq; namespace Reverse { public class Program { public static void Main(string[] args) { char[] numbers = { '1', '3', '4', '2', '6', '5' }; var result = numbers.Reverse(); foreach (var item in result) { Console.WriteLine(item); } Console.ReadLine(); } } } |
Example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | using System.Collections.Generic; using System; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { int[] intArray = new int[] { 10, 30, 50, 40,60,20,70,100 }; Console.WriteLine("Before Reverse the Data"); foreach (var number in intArray) { Console.Write(number + " "); } Console.WriteLine(); IEnumerable<int> ArrayReversedData = intArray.Reverse(); Console.WriteLine("After Reverse the Data"); foreach (var number in ArrayReversedData) { Console.Write(number + " "); } Console.ReadKey(); } } } |
Example 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | using System.Collections.Generic; using System; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { List<string> stringList = new List<string>() { "Preety", "Tiwary", "Agrawal", "Priyanka", "Dewangan"}; Console.WriteLine("Before Reverse the Data"); foreach (var name in stringList) { Console.Write(name + " "); } Console.WriteLine(); //You cannot store the data like below as this method belongs to //System.Collections.Generic namespace whose return type is void //IEnumerable<int> ArrayReversedData = stringList.Reverse(); stringList.Reverse(); Console.WriteLine("After Reverse the Data"); foreach (var name in stringList) { Console.Write(name + " "); } Console.ReadKey(); } } } |
Example 4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | using System; using System.Collections.Generic; using System.Linq; namespace LinqTutorial { class ProductStore { public string productName { get; set; } public int productPrice { get; set; } } class Program { static void Main(string[] args) { // Creating List IList<ProductStore> productList = new List<ProductStore>(); productList.Add(new ProductStore { productName = "Hard Disk", productPrice = 1280 }); productList.Add(new ProductStore { productName = "Monitor", productPrice = 3000 }); productList.Add(new ProductStore { productName = "Monitor", productPrice = 3500 }); productList.Add(new ProductStore { productName = "Monitor", productPrice = 2000 }); productList.Add(new ProductStore { productName = "SSD Disk", productPrice = 3500 }); productList.Add(new ProductStore { productName = "RAM", productPrice = 2450 }); productList.Add(new ProductStore { productName = "Processor", productPrice = 7680 }); productList.Add(new ProductStore { productName = "Bluetooth", productPrice = 540 }); productList.Add(new ProductStore { productName = "Keyboard & Mouse", productPrice = 1130 }); var result = productList.OrderBy(p => p.productPrice).Reverse(); foreach (var list in result) { Console.WriteLine("Product Name: {0} | Product Price : {1}", list.productName, list.productPrice); } Console.ReadKey(); } } } |