Sort a list and all its nested objects using LINQ
You can sort before :
organisations.ToList().ForEach(o => o.Departments = o.Departments.OrderBy(d => d.Code).ToList());
organisations.SelectMany(o => o.Departments).ToList().ForEach(d => d.Employees = d.Employees.OrderBy(e => e.Name).ToList());
And then use the list already sorted
var legalEntitiesCollectionByType = new Dictionary<string, ICollection<Organisation>>
{
{
"Institutional", organisations
.Where(x => x.Type == "Institutional")
.ToList()
},
{
"Retail", organisations
.Where(x => x.Type == "Retail")
.ToList()
}
};
NB : the sort is not in place, you can achieve this using a comparer
organisations.ToList().ForEach(o => o.Departments.Sort(CreateCustomComparison));
organisations.SelectMany(o => o.Departments).ToList().ForEach(d => d.Employees.Sort(CreateCustomComparison));
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);
You need to do all three levels of sorting inside the objects that you return, like this (I'll show only the "Retail"
, the "Institutional"
needs to be sorted in the same way):
{
"Retail", organisations
.Where(x => x.Type == "Retail")
.OrderBy(x => x.Code).ThenBy(x => x.Name)
.Select(x => new Organisation {
x.Code
, x.Type
, x.Name
, Departments = x.Departmentsd.OrderBy(d => d.Code).ThenBy(d => d.Name)
.Select(d => new Department {
d.Code
, d.Name
, Employees = d.Employees.OrderBy(e => e.Code).ThenBy(e => e.Name).ToList()
})
}).ToList()
}
Since you need to select this multiple times, you may want to wrap this code in a method, and use it from several spots, like this:
private Organisation SortedOrganisation(Organisation x) {
return new Organisation {
x.Code
, x.Type
, x.Name
, Departments = x.Departmentsd.OrderBy(d => d.Code).ThenBy(d => d.Name)
.Select(d => new Department {
d.Code
, d.Name
, Employees = d.Employees.OrderBy(e => e.Code).ThenBy(e => e.Name).ToList()
})
};
}
...
var legalEntitiesCollectionByType = new Dictionary<string, ICollection<Organisation>>
{
{
"Institutional", organisations
.Where(x => x.Type == "Institutional")
.OrderBy(x => x.Code).ThenBy(x => x.Name)
.Select(SortedOrganisation)
.ToList()
},
{
"Retail", organisations
.Where(x => x.Type == "Retail")
.OrderBy(x => x.Code).ThenBy(x => x.Name)
.Select(SortedOrganisation)
.ToList()
}
};