Reputation: 3404
I need to bring back ordered 'ExecutiveSections' which have a 'sortOrder', and chld-objects 'ExecutiveSectionMappings', which have a 'sortOrder' as well.
I need to sort both so that the Sections are in order with their respective mappings ordered below them (the mappings have the 'executives' themselves in them) So it shows on the site Section by Section with the Executives ordered correctly.
So far, I have tried:
var sections = _executiveSectionRepository.GetExecutiveSections()
.OrderBy(x => x.SortOrder)
.ThenBy(x => x.ExecutiveSectionMappings.Select(c => c.SortOrder));
and:
var sections = _executiveSectionRepository.GetExecutiveSections()
.OrderBy(x => x.SortOrder)
.ThenBy(x => x.ExecutiveSectionMappings.Select(c => c.SortOrder).FirstOrDefault());
This only orders the ExecutiveSections, not the ExecutiveSectionMappings... What am I missing?
Upvotes: 0
Views: 663
Reputation: 19646
I'm not 100% sure I understand however-
I'm assuming you have a tree-like hierarchy:
Section 1
sub-section 1
sub-section 2
Section 2
sub-section 1
sub-section 2
I'm not sure you can avoid having to sort the sub-items collections separately, so something like:
var sections = _executiveSectionRepository.GetExecutiveSections()
.OrderBy(x => x.SortOrder);
foreach (var section in sections)
{
section.ExecutiveSectionMappings = section.ExecutiveSectionMappings.OrderBy(x => x.SortOrder);
}
Upvotes: 3