Trending September 2023 # How Orderby Works In Linq With Examples? # Suggested October 2023 # Top 15 Popular | Happystarlongbien.com

Trending September 2023 # How Orderby Works In Linq With Examples? # Suggested October 2023 # Top 15 Popular

You are reading the article How Orderby Works In Linq With Examples? updated in September 2023 on the website Happystarlongbien.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Orderby Works In Linq With Examples?

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Let’s see the syntax of the LINQ-OrderBy operator as follows; it sorts the collection of elements in ascending order.

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.

{ 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,

  Var orderBy_ = from s in get_UserDetails orderby s.userName select s;

 //Method Syntax

Console.WriteLine(“tUsing LINQ_OrderBy in Default Ascending Ordern”); 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,

Var orderBy_Descending = from s in get_UserDetails orderby s.userName descending select s;

//Method Syntax

Console.WriteLine(“ntUsing LINQ_OrderBy in Descending Ordern”); 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; } { { 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 Wayn"); Console.WriteLine("tEmployee Names in Order-wisen"); Console.WriteLine("tEmployee Namesn"); foreach (var val in _ascOrderBy) { Console.WriteLine("t {0}t",val.employeeName); } Console.ReadKey(); } } }

In this program, OrderBy() is used to retrieve the employee names in ascending order; let’s see the below code

it displays the employee name in ascending order-wise. The below output makes you understand clearly.

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; } { { 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 Byn"); Console.WriteLine("tEmployee Salary with Highly Paid Members Order-wisen"); Console.WriteLine("tEmployee Salaryt Employee Name n"); foreach (var val in _resOrderBy) { Console.WriteLine("t {0}tt  {1}",val.empSalary, val.employeeName); } Console.ReadKey(); } } }

In this program, OrderByDescending () is used to retrieve the employee salary in descending order; let’s see the below code.

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 –

You're reading How Orderby Works In Linq With Examples?

Update the detailed information about How Orderby Works In Linq With Examples? on the Happystarlongbien.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!