LINQ: Sort Multiple Columns using LINQ in C#

In this article, we will learn How to Sort multiple Columns using LINQ in C# using Order by , ThenBy, OrderbyDescending, and ThenbyDescending methods.

Here’s an example of how to Sort multiple Columns using LINQ in C#.

Create an Asp.NET Core MVC or Web API Project

Assuming you have Visual Studio 2019 or Visual Studio 2022 installed, follow these steps to create a new ASP.NET Core Project

  • Open the visual studio (IDE).
  • Click on the “Create new project” option.
  • Choose “ASP.NET Core Web Application” from the list of available templates.
  • Click the Next button.
  • The configure a new project, specify the name and location of your project.
  • Click the “Next” button.
  • Then select .Net Core as the runtime and choose the version from the dropdown list at the top.
  • Make sure to uncheck the checkboxes for “Enable Docker Support” and “Configure for HTTPS” since we won’t be using authentication.
  • Click the “Create” button

Consider an example Employee class with properties like FirstName, LastName, City, and State. We’ll sort employees based on their FirstName and LastName.

Create a Model

namespace WEB_API_without_EF.Models
{
    public class Employees
    {
        public int ID { get; set; }
        public string Company { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string JobTitle { get; set; }
        public string Address { get; set;}

        public string City { get; set; }

        public string state { get; set; }

    }
}

Create a Controller

namespace WEB_API_without_EF.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    { 
[HttpGet("GetOrderBy")]
        public IActionResult GetOrderBy()
        {
            List<Employees> employees = new List<Employees>
            {
                new Employees{FirstName="Anna", LastName="Bedecs", Company="Company A", City="Seattle",state="WA"},
                new Employees{FirstName="Antonio", LastName="Gratacos Solsona", Company="Company B", City="Boston",state="MA"},
                new Employees{FirstName="Thomas" ,LastName="Axen", Company="Company C", City="Seattle",state="WA"},
                new Employees{FirstName="Christina", LastName="Lee", Company="Company D", City="New York",state="NY"},
                new Employees{FirstName="Martin" ,LastName="O’Donnell", Company="Company E", City="Minneapolis",state="MN"},
                new Employees{FirstName="Francisco", LastName="Pérez-Olaeta", Company="Company F", City="Milwaukee",state="WI"},

            };

            //Order by Ascending Order

            var OrderbyEmployeeAsc = employees.
                        OrderBy(x => x.FirstName)
                        .ThenBy(x => x.LastName)
                        .ToList();
            // Descending Order

            var OrderbyEmployeeDesc = employees.
                        OrderBy(x => x.LastName)
                        .ThenByDescending(x => x.FirstName)
                        .ToList();

            

            return Ok(OrderbyEmployeeAsc);
        }
}
}

 Always utilize ThenBy() after OrderBy() because OrderBy() generates an IOrderedEnumerable object which then exposes the methods ThenBy() and ThenByDescending(). This means that we can order by multiple columns.

Output

Sorting Multiple Columns using LINQ  in C#

See More:

Sorting Multiple Columns using LINQ  in C#

Leave a Reply

Your email address will not be published. Required fields are marked *