Sort a list and all its nested objects using LINQ

前端 未结 5 2076
情书的邮戳
情书的邮戳 2021-01-04 23:49

There is an organisation with several departments and each department has a few employees.

I have created the following object model:

public class O         


        
5条回答
  •  独厮守ぢ
    2021-01-05 00:19

    I know this is an old question, but there's a simpler way of achieving the same result:

    organisations = organisations.OrderBy(org =>
    {
       org.Departments = org.Departments
       .OrderBy(dept =>
       {
         dept.Employees = dept.Employees
                         .OrderBy(employee => employee.Code)
                         .ThenBy(employee=>employee.Name);
         return dept.Code;
       })
       .ThenBy(dept=>dept.Name);
    
       return org.Code;
    })
    .ThenBy(org=>org.Name); 
    

提交回复
热议问题