LINQ:订购子对象

时间:2015-07-10 14:56:33

标签: c# linq sorting

我需要带回有'sortOrder'的有序'ExecutiveSections'和chld-objects'ExecutiveSectionMappings',它们也有'sortOrder'。

我需要对这两个部分进行排序,以便各部分按顺序排列各自的映射(映射中有'管理员')。因此,它会在部门中逐段显示,并且管理人员已正确订购。< / p>

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

 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());

这只订购ExecutiveSections,而不是ExecutiveSectionMappings ...... 我错过了什么?

1 个答案:

答案 0 :(得分:3)

我不是100%肯定我明白 -

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

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);
}