Linq to Sql - Sort record on multiple columns in ascending or descending order

In SQL Server it's easy and we are familiar to sort orders on different columns but when it comes to Linq to SQL or Lamda expression we stuck how to sort the records, it is because we are experienced much. So let's discuss some daily use ordering of records with examples.

1. Suppose we have an Employee table and want to sort on columns say "EmployeeName" in ascending order then

  MyDataContext db = new MyDataContext();
  List<Employee> employees = (from emp in db.Employees
       orderby emp.EmployeeName
       select emp).ToList<Employee>();
  //  OR
  List<Employee> employees = db.Employees
         .OrderBy(x => x.EmployeeName)ToList<Employee>();

2. Suppose we want to sort on columns say "JoiningDate " in descending order then

  List<Employee> employees = (from emp in db.Employees
       orderby emp.JoiningDate descending
       select emp).ToList<Employee>();
  //  OR
  List<Employee> employees = db.Employees
         .OrderByDescending(x => x.JoiningDate).ToList<Employee>();

3. Suppose we want to sort on different columns say "EmployeeName" and "City" in ascending order then

  List<Employee> employees = (from emp in db.Employees
       orderby emp.EmployeeName, emp.City
       select emp).ToList<Employee>();
  //  OR
  List<Employee> employees = db.Employees
         .OrderBy(x => x.EmployeeName)
         .ThenBy(x => x.City).ToList<Employee>();

4. Suppose we want to add one more ordering on column "JoiningDate" in descending order then

  List<Employee> employees = (from emp in db.Employees
       orderby emp.EmployeeName, emp.City, emp.JoiningDate descending
       select emp).ToList<Employee>();
  //  OR
  List<Employee> employees = db.Employees
         .OrderBy(x => x.EmployeeName)
         .ThenBy(x => x.City)
         .ThenByDescending(x => x.JoiningDate).ToList<Employee>();

We can also sort records by using new, see this

var employees = db.Employees.OrderBy( x => new { x.EmployeeName, x.City, x........});

we can use here as many columns as we many we want by separating with comma (,).

Mike .Net
  • linq to sql
By Mike .Net On 30 Jan, 13  Viewed: 4,736

Other blogs you may like

Linq to SQL connection String to read from web.config file automatically

When we use Linq to SQL it creates a connection string in Settings files. When we need to create the DLL of the DataAccessLayer (DAL) for production server or any server which is not our current development server then we need to change connection string manually in Settings file, is not it would... By Montana Neyra   On 17 Jun 2012  Viewed: 5,005

MVC paging with linq to sql and html helper class

To use paging in MVC you need to first create structure to pass values form page to HTML Helper class so first create a class to keep and pass the page information public class PagingInfo { public int TotalItems { get; set; } public int ItemsPerPage { get; set; } ... By Ali Adravi   On 03 Jan 2013  Viewed: 3,500