Updated March 31, 2023
Introduction to LINQ orderby
LINQ-OrderBy sorts the values in the collection on the basis of the specified field in ascending or descending manner. By default, it sorts the collection of elements in ascending order. This is because the main purpose of the order_by operator is to re-arranging the elements in the series in ascending.
Syntax
Let’s see the syntax of the LINQ-OrderBy operator as follows; it sorts the collection of elements in ascending order.
Var names=obj_name.orderBy(s=>s.name);
The order_by method supports any data type; we can use integer, string, character, decimal, and so on.
How orderby works in LINQ?
The LINQ_OrderBy operator is mainly used to rearrange the collection of elements in the sequence in ascending order. If we want to make the collection in descending order, use the keyword descending to retrieve the collection in descending order. In the LINQ-OrderBy method, it supports both query and method syntax.
Let’s see the query syntax with examples. OrderBy sorts the values of a collection in ascending or descending order. It sorts the collection in ascending order by default because ascending keyword is optional here. So firstly, code the list of user details collection like user_name, user_age, and so on.
Var get_UserDetails = new List<UserData>()
{
new UserData { userName = "Smith", userAge = 23 },
new UserData { userName = "Rio", userAge = 30},
new UserData { userName = "Jack", userAge = 41},
new UserData { userName = "Danial", userAge = 25 },
new UserData { userName = "Peter", userAge = 32 },
};
The default sort is ascending order because the optional keyword is ascending here and see the below example of ascending order in query syntax and also method syntax as follows,
//Query Syntax
Var orderBy_ = from s in get_UserDetails
orderby s.userName
select s;
//Method Syntax
Var orderBy_ = get_UserDetails.OrderBy(s => s.userName);
Console.WriteLine("\tUsing LINQ_OrderBy in Default Ascending Order\n");
foreach (var order_by in orderBy_)
{
Console.WriteLine("\t- " + order_by.userName);
}
If we want to sort in descending order, use the descending keyword to sort the collection of elements in descending order below the example is shown,
// Query Syntax
Var orderBy_Descending = from s in get_UserDetails
orderby s.userName descending
select s;
//Method Syntax
Var orderBy_Descending = get_UserDetails.OrderByDescending(s => s.userName);
Console.WriteLine("\n\tUsing LINQ_OrderBy in Descending Order\n");
foreach (var order_by in orderBy_Descending)
{
Console.WriteLine("\t- " + order_by.userName);
}
In method syntax, the keyword ‘descending’ is not allowed to sort the collection; instead, we go with the OrderByDescending() method is used to sort in descending order. Likewise, the OrderByDescending() method is not valid in query syntax because it uses the ascending and descending attributes shown in the example above its valid only in method syntax.
Examples of LINQ orderby
The LINQ_OrderBy operator is mainly used to rearrange the collection of elements in the sequence in ascending order. The order_by method supports any data type; we can use integer, string, character, decimal, and so on. The default sort is ascending order because the optional keyword is ascending here and 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_LinqOrderBy
{
class Program_LINQ_OrderBy
{
public class EmployeeDetails
{
public int employeeID
{
get;
set;
}
public string employeeName
{
get;
set;
}
public string employeeQualification
{
get;
set;
}
public int empSalary { get; set; }
public int DepartmentID { get; set; }
public int empAge { get; set; }
public static List<EmployeeDetails> Get_Employees()
{
return new List<EmployeeDetails>()
{
new EmployeeDetails() {employeeID = 1000, employeeName = "Henry",DepartmentID=1,employeeQualification = "MCA", empAge=21,empSalary=25000},
new EmployeeDetails() {employeeID = 1005, employeeName = "Remo", DepartmentID=2,employeeQualification = "B.E",empAge=23, empSalary=47000},
new EmployeeDetails() {employeeID = 1001, employeeName = "Smith",DepartmentID=1,employeeQualification = "MCA",empAge=21, empSalary=60000},
new EmployeeDetails() {employeeID = 1007, employeeName = "Rio",DepartmentID=3, employeeQualification = "B.E",empAge=27,empSalary=35000},
new EmployeeDetails() {employeeID = 1003, employeeName = "Jack",DepartmentID=1, employeeQualification = "M.Sc",empAge=21, empSalary=30000},
new EmployeeDetails() {employeeID = 1004, employeeName = "Peter",DepartmentID=3, employeeQualification = "B.E",empAge=27, empSalary=52000},
};
}
}
static void Main(string[] args)
{
Console.WriteLine("\tLINQ_OrderBy in Ascending Way\n");
// OrderBy() used to sort the employee Names in Ascending order
var _ascOrderBy = EmployeeDetails.Get_Employees().OrderBy(a => a.employeeName);
Console.WriteLine("\tEmployee Names in Order-wise\n");
Console.WriteLine("\tEmployee Names\n");
Console.WriteLine("\t-------------- \n");
foreach (var val in _ascOrderBy)
{
Console.WriteLine("\t {0}\t",val.employeeName);
}
Console.WriteLine("\n\t--------------\n");
Console.ReadKey();
}
}
}
In this program, OrderBy() is used to retrieve the employee names in ascending order; let’s see the below code
var _ascOrderBy = EmployeeDetails.Get_Employees().OrderBy(a => a.employeeName)
it displays the employee name in ascending order-wise. The below output makes you understand clearly.
Output:
Example #2
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_LinqOrderBy
{
class Program_LINQ_OrderBy
{
public class EmployeeDetails
{
public int employeeID
{
get;
set;
}
public string employeeName
{
get;
set;
}
public string employeeQualification
{
get;
set;
}
public int empSalary { get; set; }
public int DepartmentID { get; set; }
public int empAge { get; set; }
public static List<EmployeeDetails> Get_Employees()
{
return new List<EmployeeDetails>()
{
new EmployeeDetails() {employeeID = 1000, employeeName = "Henry",DepartmentID=1,employeeQualification = "MCA", empAge=21,empSalary=25000},
new EmployeeDetails() {employeeID = 1005, employeeName = "Remo", DepartmentID=2,employeeQualification = "B.E",empAge=23, empSalary=47000},
new EmployeeDetails() {employeeID = 1001, employeeName = "Smith",DepartmentID=1,employeeQualification = "MCA",empAge=21, empSalary=60000},
new EmployeeDetails() {employeeID = 1007, employeeName = "Rio",DepartmentID=3, employeeQualification = "B.E",empAge=27,empSalary=35000},
new EmployeeDetails() {employeeID = 1003, employeeName = "Jack",DepartmentID=1, employeeQualification = "M.Sc",empAge=21, empSalary=30000},
new EmployeeDetails() {employeeID = 1004, employeeName = "Peter",DepartmentID=3, employeeQualification = "B.E",empAge=27, empSalary=52000},
};
}
}
static void Main(string[] args)
{
Console.WriteLine("\tUsing Linq- Order By\n");
// OrderByDescending() used to sort the employee salary in descending order
var _resOrderBy = EmployeeDetails.Get_Employees().OrderByDescending(e => e.empSalary);
Console.WriteLine("\tEmployee Salary with Highly Paid Members Order-wise\n");
Console.WriteLine("\tEmployee Salary\t Employee Name \n");
Console.WriteLine("\t--------------\t ------------- \n");
foreach (var val in _resOrderBy)
{
Console.WriteLine("\t {0}\t\t {1}",val.empSalary, val.employeeName);
}
Console.WriteLine("\n\t--------------\t ------------- \n");
Console.ReadKey();
}
}
}
In this program, OrderByDescending () is used to retrieve the employee salary in descending order; let’s see the below code.
var _resOrderBy = EmployeeDetails.Get_Employees().OrderByDescending(e => e.empSalary);
Here we need to display the highly paid employees in order-wise, so we used this OrderByDescending () method to display highly paid salary with their names. The below output makes you understand clearly.
Output:
Recommended Articles
This is a guide to LINQ orderby. Here we discuss the use of the Linq OrderBy Method in C# with several examples programmatically. You may also have a look at the following articles to learn more –