Saturday, August 24, 2019

Entity Framework Core ORDER BY (Sorting)

In this post we shall see on how to sort the results from a table using LINQ Query, this is similar to the ORDER BY clause in SQL Server. In EF Core LINQ queries we can achieve this by using OrderBy.

Let us get the list of Employees in the Employee table sorted by the Name of the employee, this can be done using the following LINQ query.

SQL Query
SELECT
* FROM [dbo].[Employee] ORDER BY Name

LINQ Query
var Employee = _context.Employee.OrderBy(e => e.Name);


SQL Query
SELECT
* FROM [dbo].[Employee] ORDER BY Name DESC


LINQ Query
var Employee = _context.Employee.OrderByDescending(e => e.Name);

Search Flipkart Products:
Flipkart.com

No comments: