Updated April 4, 2023
Introduction to LINQ OrderBy Desc
LINQ OrderBy Desc operator sorting the elements in descending order, it returns the collection in descending order. The values in the sequence of element returns the result in descending order based on the specific field. It is not valid in query syntax it is available only in the method syntax. The main function of OrderBy Desc is to re-arrange the sequence of elements in descending order.
Syntax:
Let’s see the syntax of OrderBy Descending operator as follows,
public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);
Here we are using the operator OrderByDescending which is used to sort the sequence of elements in descending order.
How OrderBy Desc works in LINQ?
As with OrderBy method, we can also use the OrderByDescending method for all data types such as character, integer, string, float, and so on. Let’s see one integer collection as follows,
List<int> _numList = new List<int>() {100,220,430,440,550,330, 990} ;
var methodSyntax=_ numList.OrderByDescending(n=>n);
foreach(var res in val)
{
Console.Write(res+"");
}
For this code, we will get the result in descending orders as follows “990, 550, 440, 430, 330, 220, and 100 “.
The values in the sequence of element returns the result in descending order based on the specific field. The objective of OrderBy Desc is to re-arrange the sequence of elements in descending order. Let’s see some working flow of OrderByDescending as follows,
IList<Customer> CustomerList = new List<Customer>() {
new Customer() { CustomerID = 1001, CustomerName = "Mithun", Age = 28 } ,
new Customer() { CustomerID = 1002, CustomerName = "Jack", Age = 25 } ,
new Customer() { CustomerID = 1003, CustomerName = "Gates", Age = 15 } ,
new Customer() { CustomerID = 1004, CustomerName = "Shasha" , Age = 30 } ,
new Customer() { CustomerID = 1005, CustomerName = "Jhon" , Age = 29 }
};
var Customer_InDescOrder = CustomerList.OrderByDescending(c => c.CustomerName);
To understand the above lines of code, it will list the customer names in descending order by using the operator OrderByDescending and this operator will not support the query syntax because it’s the only keyword OrderByDescending so we can’t make use of it, it is supported by only Method syntax. If we want to use in query syntax of descending order process uses the descending keyword instead.
Examples
In LINQ_OrderByDescending method used to re-arrange the sequence of elements in descending order. OrderByDescending method supports all data types such as character, integer, string, float, and so on. It is not valid in query syntax it is available only in the method syntax. Let’s see the below example of ascending order in method syntax as follows,
Example #1
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_LinqOrderByDesc
{
class Program_LINQ_OrderByDesc
{
public class E_Details
{
public int E_ID
{
get;
set;
}
public string E_Name
{
get;
set;
}
public string E_Qualif
{
get;
set;
}
public int E_Salary { get; set; }
public int D_ID { get; set; }
public int E_Age { get; set; }
public static List<E_Details> EmpRecords()
{
return new List<E_Details>()
{
new E_Details() {E_ID = 1000, E_Name = "Henry",D_ID=1,E_Qualif = "MCA", E_Age=21,E_Salary=25000},
new E_Details() {E_ID = 1005, E_Name = "Remo", D_ID=2,E_Qualif = "B.E",E_Age=23, E_Salary=47000},
new E_Details() {E_ID = 1001, E_Name = "Smith",D_ID=1,E_Qualif = "MCA",E_Age=21, E_Salary=60000},
new E_Details() {E_ID = 1007, E_Name = "Rio",D_ID=3, E_Qualif = "B.E",E_Age=27,E_Salary=35000},
new E_Details() {E_ID = 1003, E_Name = "Jack",D_ID=1, E_Qualif = "M.Sc",E_Age=21, E_Salary=30000},
new E_Details() {E_ID = 1004, E_Name = "Peter",D_ID=3, E_Qualif = "B.E",E_Age=27, E_Salary=52000},
};
}
}
static void Main(string[] args)
{
Console.WriteLine("\tLINQ_OrderBy in Descending Order\n");
// here OrderByDescending() is used to sort the employee Names in Descending order
var _descOrderBy = E_Details.EmpRecords().OrderByDescending(emp => emp.E_Name);
Console.WriteLine("\tEmployee Names in Descending Order-wise\n");
Console.WriteLine("\tEmployee Names\n");
Console.WriteLine("\t-------------- \n");
foreach (var val in _descOrderBy)
{
Console.WriteLine("\t {0}\t", val.E_Name);
}
Console.WriteLine("\n\t--------------\n");
Console.ReadKey();
}
}
}
In this above program OrderByDescending() is used to get back the details of employee names in descending order by using the operator, see the below code
var _descOrderBy = E_Details.EmpRecords().OrderByDescending(emp => emp.E_Name);
In the above line, it describes the employee names in descending order. Let’s check the below output as follows,
Output:
Example #2
Code:
using System;
using System.Collections.Generic;
using System. Linq;
using System. Text;
using System.Threading.Tasks;
namespace Console_LinqOrderByDesc
{
class Program_LINQ_OrderByDesc
{
public class E_Details
{
public int E_ID
{
get;
set;
}
public string E_Name
{
get;
set;
}
public string E_Qualif
{
get;
set;
}
public int E_Salary { get; set; }
public int D_ID { get; set; }
public int E_Age { get; set; }
public static List<E_Details> EmpRecords()
{
return new List<E_Details>()
{
new E_Details() {E_ID = 1000, E_Name = "Shasha",D_ID=1,E_Qualif = "MCA", E_Age=21,E_Salary=75000},
new E_Details() {E_ID = 1005, E_Name = "Lalith", D_ID=2,E_Qualif = "B.E",E_Age=23, E_Salary=56000},
new E_Details() {E_ID = 1001, E_Name = "David",D_ID=1,E_Qualif = "MCA",E_Age=21, E_Salary=35000},
new E_Details() {E_ID = 1007, E_Name = "James",D_ID=3, E_Qualif = "B.E",E_Age=27,E_Salary=48000},
new E_Details() {E_ID = 1003, E_Name = "Rockey",D_ID=1, E_Qualif = "M.Sc",E_Age=21, E_Salary=52000},
new E_Details() {E_ID = 1004, E_Name = "Cheenu",D_ID=3, E_Qualif = "B.E",E_Age=27, E_Salary=89000},
};
}
}
static void Main(string[] args)
{
Console.WriteLine("\tUsing Linq- Order By Descending\n");
// OrderByDescending() used to sort the employee salary in descending order
var Desc_OrderBy = E_Details.EmpRecords().OrderByDescending(e => e.E_Salary);
Console.WriteLine("\tSalaries of Employee with their Names in Order-wise\n");
Console.WriteLine("\tEmployee Salary\t Employee Name \n");
Console.WriteLine("\t--------------\t ------------- \n");
foreach (var res in Desc_OrderBy)
{
Console.WriteLine("\t {0}\t\t {1}", res.E_Salary, res.E_Name);
}
Console.WriteLine("\n\t--------------\t ------------- \n");
Console.ReadKey();
}
}
}
In the above coding the operator OrderByDescending() is used to get back the salaries of each and every employee in descending order, let’s check the below code
var Desc_OrderBy = E_Details.EmpRecords().OrderByDescending(e => e.E_Salary);
in the above line of code describes the higher salaried employee name with their corresponding names in descending order by using this OrderByDescending () method, let’s check the below output for better understanding,
Output:
Conclusion
The article is about the LINQ operator called OrderByDescending which is used to re-arrange the collection of elements in descending order, here in this article I have explained various examples programmatically. Hope the article helps out for your better understanding with examples.
Recommended Articles
This is a guide to LINQ OrderBy Desc. Here we discuss the Introduction, syntax, How OrderBy Desc works in LINQ? and examples for better understanding. You may also have a look at the following articles to learn more –