LINQ:排序子对象

本文关键字:对象 排序 LINQ | 更新日期: 2023-09-27 18:02:18

我需要带回有序的'ExecutiveSections',其中有一个'sortOrder',和子对象'ExecutiveSectionMappings',其中有一个'sortOrder'为好。

我需要对两者进行排序,以便Sections与它们各自的映射在它们下面排序(映射中有"执行人员"本身),因此它在网站上显示Section by Section与执行人员正确排序。

到目前为止,我已经试过了:

 var sections = _executiveSectionRepository.GetExecutiveSections()
            .OrderBy(x => x.SortOrder)
            .ThenBy(x => x.ExecutiveSectionMappings.Select(c => c.SortOrder));

:

var sections = _executiveSectionRepository.GetExecutiveSections()
            .OrderBy(x => x.SortOrder)
            .ThenBy(x => x.ExecutiveSectionMappings.Select(c => c.SortOrder).FirstOrDefault());

这只命令executivesection,而不是ExecutiveSectionMappings…我错过了什么?

LINQ:排序子对象

我不能百分之百地肯定我理解了-

我假设你有一个树状的层次结构:

Section 1
    sub-section 1
    sub-section 2
Section 2
    sub-section 1
    sub-section 2

我不确定你是否可以避免将子项集合分开排序,所以像这样:

var sections = _executiveSectionRepository.GetExecutiveSections()
            .OrderBy(x => x.SortOrder);
foreach (var section in sections)
{
    section.ExecutiveSectionMappings = section.ExecutiveSectionMappings.OrderBy(x => x.SortOrder);
}