2015-07-10 76 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

謝謝!這工作 –