Loading

Microsoft .Net 6 LINQ

What is Ordering Operators in LINQ?. The Complete Microsoft .Net 6 LINQ Developer Course 2023 [Videos].

In this article, I am going to discuss the Ordering Operators in LINQ with examples. Please read our previous article where we discussed the Union Method in C# with examples. At the end of this article, you will learn the following two things.

Ordering Operators in LINQ

What Are Ordering Operators?

In simple terms, we can say that Ordering is nothing but a process to manage the data in a particular order. It is not changing the data or output rather this operation arranges the data in a particular order i.e. either ascending order or descending order.

In this case, the count is going to be the same but the order of the element is going to change.

We sort the data in two ways i.e. ascending or descending order. The order may be integer-based or any other data type based. For example

  1. Name of Cities of a particular state in alphabetical order.
  2. Students order by Roll Number in a class.

It is also possible to order based on multiple columns like Employee First and Last Name in ascending order while the Salary is on descending order.

What are the Methods available in Linq for Sorting the data?

There are five methods provided by LINQ to sort the data. They are as follows

  1. What is Linq OrderBy Method?
  2. Example of Linq OrderBy Method using both Method and Query Syntax.
  3. How to use Linq OrderBy Method with Complex Type in C#?
  4. How to use the OrderBy method along with the Filtering method?
What is Linq OrderBy Method?

The Linq OrderBy method in C# is used to sort the data in ascending order. The most important point that you need to keep in mind is this method is not going to change the data rather it is just changing the order of the data.

You can use the OrderBy method on any data type i.e. you can use character, string, decimal, integer, etc. Let us understand the use of the LINQ OrderBy method in C# using both query syntax and method syntax.

Example1: Working with integer data

In the below example we have a collection of integer data. And we sort the data in ascending order using the OrderBy method.

using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
List<int> intList = new List<int>() { 10, 45, 35, 29, 100, 69, 58, 50 };
Console.WriteLine("Before Sorting : ");
foreach (var item in intList)
{
Console.Write(item + " ");
}
//Sorting the data in Ascending Order
//Using Method Syntax
var MS = intList.OrderBy(num => num);
//Using Query Syntax
var QS = (from num in intList
orderby num
select num).ToList();
Console.WriteLine();
Console.WriteLine("After Sorting : ");
foreach (var item in QS)
{
Console.Write(item + " ");
}
Console.ReadKey();
}
}
}

Output:

Linq OrderBy Method with integer data Example

Exampe2: Working with string data.

In the below example we have a collection of string names. We then sort the data in ascending order using both method and query syntax.

using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
List<string> stringList = new List<string>() { "Preety", "Tiwary", "Agrawal", "Priyanka", "Dewangan",
"Hina","Kumar","Manoj", "Rout", "James"};
//Using Method Syntax
var MS = stringList.OrderBy(name => name);
//Using Query Syntax
var QS = (from name in stringList
orderby name ascending
select name).ToList();
foreach (var item in MS)
{
Console.WriteLine(item + " ");
}
Console.ReadKey();
}
}
}

Note: In query syntax, while we are sorting the data in ascending order then the use of ascending operator is optional. That means if we are not specifying anything then by default it is ascending. So the following two statements are the same.

Linq OrderBy Method in C#

Using LINQ OrderBy Method with Complex type:

We are going to work with the following Student class. So, create a class file with the name Student.cs and then copy and paste the following code in it.

using System.Collections.Generic;
namespace LINQDemo
{
public class Student
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Branch { get; set; }
public static List<Student> GetAllStudents()
{
List<Student> listStudents = new List<Student>()
{
new Student{ID= 101,FirstName = "Preety",LastName = "Tiwary",Branch = "CSE"},
new Student{ID= 102,FirstName = "Preety",LastName = "Agrawal",Branch = "ETC"},
new Student{ID= 103,FirstName = "Priyanka",LastName = "Dewangan",Branch = "ETC"},
new Student{ID= 104,FirstName = "Hina",LastName = "Sharma",Branch = "ETC"},
new Student{ID= 105,FirstName = "Anugrag",LastName = "Mohanty",Branch = "CSE"},
new Student{ID= 106,FirstName = "Anurag",LastName = "Sharma",Branch = "CSE"},
new Student{ID= 107,FirstName = "Pranaya",LastName = "Kumar",Branch = "CSE"},
new Student{ID= 108,FirstName = "Manoj",LastName = "Kumar",Branch = "ETC"},
new Student{ID= 109,FirstName = "Pranaya",LastName = "Rout",Branch = "ETC"},
new Student{ID= 110,FirstName = "Saurav",LastName = "Rout",Branch = "CSE"}
};
return listStudents;
}
}
}

As you can see, we created the Student class with four properties such as ID, FirstName, LastName, and Brach. We then created one method (i.e. GetAllStudents) within the same class which is going to return a list of all students.

Example3: Sorting the Data in Ascending Order

Here, we want to sort the data based on the Branch of the Student in ascending order.

using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
//Method Syntax
var MS = Student.GetAllStudents().OrderBy(x => x.Branch).ToList();
//Query Syntax
var QS = (from std in Student.GetAllStudents()
orderby std.Branch
select std);
foreach (var student in MS)
{
Console.WriteLine(" Branch: " + student.Branch + ", Name :" + student.FirstName + " " + student.LastName );
}
Console.ReadKey();
}
}
}

Output:

The OrderBy Method in C# with Complex Type

Example4: Sorting with Filtering.

Now we need to fetch only the CSE branch students and then we need to sort the data based on the FirstName in ascending order.

Note: The most important point that you need to remember is, you need to use the Where method before the OrderBy method. The following example shows the above using both query and method syntax.

using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
//Method Syntax
var MS = Student.GetAllStudents()
.Where(std => std.Branch.ToUpper() == "CSE")
.OrderBy(x => x.FirstName).ToList();
//Query Syntax
var QS = (from std in Student.GetAllStudents()
where std.Branch.ToUpper() == "CSE"
orderby std.FirstName
select std);
foreach (var student in QS)
{
Console.WriteLine(" Branch: " + student.Branch + ", Name :" + student.FirstName + " " + student.LastName );
}
Console.ReadKey();
}
}
}

Output:

The OrderBy Method with Filtering in C#

See All

Comments (468 Comments)

Submit Your Comment

See All Posts

Related Posts

Microsoft .Net 6 LINQ / Blog

What is Microsoft .Net 6 LINQ?

LINQ stands for Language-Integrated Query and it is a powerful query language that was introduced with .Net 3.5 & Visual Studio 2008. You can use LINQ with C# or VB to query different types of data sources such as SQL, XML, In memory objects, etc.
14-Feb-2022 /38 /468

Microsoft .Net 6 LINQ / Blog

What is Architecture of LINQ?

In this article, I am going to discuss the Architecture of LINQ. The term LINQ stands for Language Integrated Query and it is pronounced as LINK. Nowadays the use of use LINQ increasing rapidly. So, as a developer, you should understand the Linq and its architecture. At the end of this article, you will have a very good understanding of the following pointers.
14-Feb-2022 /38 /468

Microsoft .Net 6 LINQ / Blog

How to write Different Ways to Write LINQ Query?

In this article, I am going to discuss the Different Ways to write LINQ Query i.e. Linq Query Syntax and Linq Method Syntax with examples. Please read our previous article where we discussed the Architecture of LINQ i.e. how LINQ works. In this article, we are going to discuss the following pointers.
14-Feb-2022 /38 /468