In this tutorial we’ll learn Linq OrderBy and OrderByDescending Operators.
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.
OrderBy and OrderByDescending, both are a sorting operator that sorts the elements in a collection. OrderBy sorts elements in ascending order, whereas OrderByDescending Operator sorts element in descending order.
LINQ – OrderBy Example:
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 43 44 45 46 | 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 = "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 }); //LINQ Query Syntax var result = from p in productList orderby p.productPrice select p; //LINQ Method Syntax. Uncomment it to see the result. //var result = productList.OrderBy(p => p.productPrice); foreach (var list in result) { Console.WriteLine("Product Name: {0} | Product Price : {1}", list.productName, list.productPrice); } Console.ReadKey(); } } } |
Output:
1 2 3 4 5 6 7 8 9 | Product Name: Bluetooth | Product Price : 540 Product Name: Keyboard & Mouse | Product Price : 1130 Product Name: Hard Disk | Product Price : 1280 Product Name: RAM | Product Price : 2450 Product Name: Monitor | Product Price : 3000 Product Name: SSD Disk | Product Price : 3500 Product Name: Processor | Product Price : 7680 |
LINQ – OrderByDescending Example:
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 43 44 45 46 | 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 = "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 }); //LINQ Query Syntax var result = from p in productList orderby p.productPrice descending select p; //LINQ Method Syntax. Uncomment it to see the result. //var result = productList.OrderByDescending(p => p.productPrice); foreach (var list in result) { Console.WriteLine("Product Name: {0} | Product Price : {1}", list.productName, list.productPrice); } Console.ReadKey(); } } } |