Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple Orderby and Sort

Tags:

c#

lambda

linq

I have 2 classes:

 Employee
 EmployeeDetails

I would like to have a list with first ordering of Employee then sort it, then EmployeeDetails orderby and then sort it.

I was thinking of something like this:

var result = _db.Employee
              .OrderBy( e => e.EmpName )
              .Sort('DepartmentId') // using the diff property
              .ThenBy( ed => ed.EmpAddress )
              .Sort('PostCode'); // using the diff property

I need to first order by EmpName from Employee then Sort it after from those resultset I want to orderby EmpAddress and Sort it and then return the resultset.

Is this even possible? Can it be done without using Lambdas? Are the other approaches?

like image 666
patel.milanb Avatar asked Feb 19 '23 22:02

patel.milanb


2 Answers

Is the following code what you're looking for?

var result = _db.Employee
                .OrderBy(e => e.EmpName)
                .ThenBy(e => e.DeptartmentId)
                .ThenBy(e => e.EmpAddresss)
                .ThenBy(e => e.PostCode);
like image 62
Spectre87 Avatar answered Feb 23 '23 13:02

Spectre87


You are looking for:

var result = _db.Employee
              .OrderBy( e => e.EmpName )  //OrderBy and SortBy empname  Ascending
              .ThenBy( ed => ed.EmpAddress ); //orderby address ascending
like image 33
Mahmoud Gamal Avatar answered Feb 23 '23 12:02

Mahmoud Gamal