2015-07-10 92 views
0

我需要带回有序的'ExecutiveSections',它有一个'sortOrder',以及chld-objects'ExecutiveSectionMappings',它们也有一个'sortOrder'。LINQ:订购子对象

我需要对两者进行排序,以便各部分按照它们各自的映射排列在它们下面(映射中有'管理人员'本身)。因此它显示在网站上,部分由管理人员按正确顺序排列。

到目前为止,我曾尝试:

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 ... 我缺少什么?

+0

为什么不能'GetExecutiveSections'被修改为返回正确排序的子对象?在LINQ中修改子集合需要重新创建整个集合。 –

回答

2

我不是100%肯定我明白however-

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

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

谢谢!这工作 –