多个Orderby和Sort

pat*_*anb 4 c# linq lambda

我有2个班:

 Employee
 EmployeeDetails
Run Code Online (Sandbox Code Playgroud)

我想有一个列表,首先订购Employee然后对它进行排序,然后是EmployeeDetails orderby然后对它进行排序.

我在考虑这样的事情:

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

我需要首先EmpName从Employee 订购然后从那些结果集中排序我想要通过EmpAddress排序并对其进行排序然后返回结果集.

这甚至可能吗?可以在不使用Lambdas的情况下完成吗?是其他方法吗?

Ale*_*imp 5

以下代码是您要找的?

var result = _db.Employee
                .OrderBy(e => e.EmpName)
                .ThenBy(e => e.DeptartmentId)
                .ThenBy(e => e.EmpAddresss)
                .ThenBy(e => e.PostCode);
Run Code Online (Sandbox Code Playgroud)


Mah*_*mal 1

您正在寻找:

var result = _db.Employee
              .OrderBy( e => e.EmpName )  //OrderBy and SortBy empname  Ascending
              .ThenBy( ed => ed.EmpAddress ); //orderby address ascending
Run Code Online (Sandbox Code Playgroud)