![]() |
| Sorting using ASP.NET Core Web API and EF Core |
What is Sorting?
Sorting is the process of arranging items in a particular order such as ascending or descending order. It can be applied to various data types including numbers, strings, objects, and more complex data structures. It presents the data in a more readable and organized way and structures the data in a meaningful way, which is useful in data analysis, reporting, etc.
Let's Implement Sorting in ASP.NET Core Web API
Add sorting params in IEmployeeService
GetAllEmployees method in IEmployeeService interface. Task<PaginatedListModel<EmployeeModel>> GetAllEmployees(int pageIndex, int pageSize, FilterModel filters, string sortBy = "id", string sortOrder = "desc");Add Sorting functionality on GetEmployee method in the EmployeeService Class
public async Task<PaginatedListModel<EmployeeModel>> GetAllEmployees(int pageIndex, int pageSize, FilterModel filters, string sortBy = "id", string sortOrder = "desc")
{
var employees = employeeDBContext.Employees.AsQueryable();
//========== Filters start =================
employees = employees
.Where(x => string.IsNullOrWhiteSpace(filters.Designation) || x.Designation == filters.Designation)
.Where(x => string.IsNullOrWhiteSpace(filters.Department) || x.Department == filters.Department)
.Where(x => !filters.DobStartDate.HasValue || x.DOB >= filters.DobStartDate)
.Where(x => !filters.DobEndDate.HasValue || x.DOB <= filters.DobEndDate)
.Where(x => string.IsNullOrWhiteSpace(filters.SearchKeyword) || x.Name.Contains(filters.SearchKeyword));
//============ Filters end =================
var totalRecords = await employees.CountAsync();
//============ Sorting ===================
employees = SortEmployeeRecords(employees, sortBy, sortOrder);
//=========== Sorting end ===============
var employeesList = await employees.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
return new PaginatedListModel<EmployeeModel>(employeesList, totalRecords, pageIndex, pageSize);
}Add sortEmployeeRecords method
private IQueryable<EmployeeModel> SortEmployeeRecords(IQueryable<EmployeeModel> employees, string sortBy, string sortOrder)
{
Expression<Func<EmployeeModel, object>> sortColumn = sortBy switch
{
"name" => x => x.Name,
"dob" => x => x.DOB,
_ => x => x.Id
};
if(sortOrder == "desc")
{
employees = employees.OrderByDescending(sortColumn);
}
else
{
employees = employees.OrderBy(sortColumn);
}
return employees;
} Above we've implemented sorting logic under the private method SortEmployeeRecords(), which is returning the sorting expression based on the sortBy field, then it applies the sorting based on sort order. This implementation is simple and clean and easy to implement. However there are multiple ways we can implement sorting. The above approach is one of these.
Note: We've added totalRecods count before the sort. The reason behind this is we don't want to include sorting in count. The totalRecords count only return the no. of rows present in the table for that search criteria. Adding sorting to the count will unnecessarily add db overheads.
As we know we can implement sorting in multiple ways, if we don't want the above implementation, there is another solution using System.Linq.Dynamic.Core, which will create dynamic sorting.
Implement sorting using System.Linq.Dynamic.Core
Add NuGet Package
Update the GetEmployeeMethod() in EmployeeService
SortEmployeeRecords(employees, sortBy, sortOrder)from GetEmployee method as we're going to use dynamic sorting using DynamicCore.employees = employees.OrderBy($"{sortBy} {sortOrder}");We no longer need the SortEmployeeRecords method at all. The above line will handle sorting for all the fields using DynamicCore. It will internally create the expression tree and apply the OrderBy logic. This will be efficient and less error prone when working with large no. of columns.
Update the Get() action method in controller
// GET: api/<EmployeesController>
[HttpGet]
public async Task<IActionResult> Get([FromQuery] FilterModel filters, int pageIndex = 1, int pageSize = 10, string sortBy = "id", string sortOrder = "desc")
{
var results = await _employeeService.GetAllEmployees(pageIndex, pageSize, filters, sortBy, sortOrder);
return Ok(results);
}Our sorting logic is implemented on the controller method which is returning list of emplyees, It will take sortBy and sortOrder as query param. So, we've to pass these values to that method and based on that it will sort and order the results. Logic is handled by the service method.
By, default it will pass id as sortBy and desc as sort order to the service method.
That is all we need for sorting.

Comments
Post a Comment