👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Using LINQ OrderBy to Sort Data

Using LINQ OrderBy to Sort Data

Author - Abdul Rahman (Bhai)

LINQ

26 Articles

Improve

Table of Contents

  1. What we gonna do?
  2. Why we gonna do?
  3. How we gonna do?
  4. Summary

What we gonna do?

In this article, let's learn about how to use OrderBy in LINQ in .NET.

Note: If you have not done so already, I recommend you read the article on Using LINQ to Select and Project Data.

We can use LINQ OrderBy to sort data within collections. ThenBy can be chained to sort by more than one column. Sorting can be done in both ascending and descending order. Default sort is done in ascending order.

Why we gonna do?

LINQ has the following sort methods which can be chained and used in any combination.

  • OrderBy()
  • ThenBy()
  • OrderByDescending()
  • ThenByDescending()

How we gonna do?

TLDR

linq orderby in dotnet

OrderBy Single Property

The following example shows how to sort Product class by Name in ascending order.


List<Product> products = GetProducts();

//Method Syntax
List<Product> sortProducts = products
			     .OrderBy(product => product.Name)
		             .ToList();

//Query Syntax
List<Product> sortProducts = (from product in products
	 	   	     orderby product.Name)
			     .ToList();
        

OrderBy Single Property in Descending

The following example shows how to sort Product class by Name in descending order.


List<Product> products = GetProducts();

//Method Syntax
List<Product> sortProducts = products
			     .OrderByDescending(product => product.Name)
		             .ToList();

//Query Syntax
List<Product> sortProducts = (from product in products
	 	   	     orderby product.Name descending)
			     .ToList();
        

OrderBy Multiple Properties

The following example shows how to sort Product class with more than one property in ascending order.


List<Product> products = GetProducts();

//Method Syntax
List<Product> sortProducts = products
			     .OrderBy(product => product.Name)
           		     .ThenBy(product => product.Size)
		             .ToList();

//Query Syntax
List<Product> sortProducts = (from product in products
	 	   	     orderby product.Name, product.Size)
			     .ToList();
        

OrderBy Multiple Properties in Descending

The following example shows how to sort Product class with more than one property in descending order.


List<Product> products = GetProducts();

//Method Syntax
List<Product> sortProducts = products
			     .OrderByDescending(product => product.Name)
           		     .ThenByDescending(product => product.Size)
		             .ToList();

//Query Syntax
List<Product> sortProducts = (from product in products
	 	   	     orderby product.Name descending, product.Size descending)
			     .ToList();
        
Demo Space

Summary

In this article we learn't how to sort data within collection using LINQ. We also learn't how to sort with multiple fields. All these can be used with any IEnumerable or IQueryable types.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • LINQ
  • OrderBy
  • ThenBy
  • OrderByDescending
  • ThenByDescending
  • Sort
  • Ascending
  • Descending