2016-08-08 58 views
1

以下代碼用於創建我公司提供的服務下拉列表。這些服務正在從我們的數據庫中提取,我硬編碼並在列表中添加了一個名爲「SSN Trace」的項目。問題在於該項目仍然顯示在列表的末尾,而不是與其他列表項目按字母順序排列。誰能幫忙?將項目添加到列表中,但不會按字母順序排序

public List<SelectListItem> createProductsDropdownForTransReport() 
     { 
      var resultsOfProductsSearch = findAllByEnumSet(EnumLookup.EnumSetType.SterlingWestProducts); 

      var transanctionsReportProducts = resultsOfProductsSearch 
       .Where(el => el.Ordinal != 95 && el.Ordinal != 253) 
       .Select(el => new SelectListItem { Text = el.Text, Value = el.Text }) 
       .OrderBy(el => el.Text) 
       .ToList(); 

      transanctionsReportProducts.Add(new SelectListItem { Text = "SSN Trace", Value = "SSN Trace" }); 

      var allTransReportProductsOption = new SelectListItem 
      { 
       Text = "All", 
       Value = String.Join(" | ", transanctionsReportProducts.Select(x => x.Text)) 
     }; 

      transanctionsReportProducts.Insert(0, allTransReportProductsOption); 

      transanctionsReportProducts.OrderBy(el => el.Text); 

      return transanctionsReportProducts; 
     } 

正確的代碼:

public IEnumerable<SelectListItem> createProductsDropdownForTransReport() 
     { 
      var resultsOfProductsSearch = findAllByEnumSet(EnumLookup.EnumSetType.SterlingWestProducts); 

      var transanctionsReportProducts = resultsOfProductsSearch 
       .Where(el => el.Ordinal != 95 && el.Ordinal != 253) 
       .Select(el => new SelectListItem { Text = el.Text, Value = el.Text }).ToList(); 



      transanctionsReportProducts.Add(new SelectListItem { Text = "SSN Trace", Value = "SSN Trace" }); 

      transanctionsReportProducts = transanctionsReportProducts.OrderBy(el => el.Text).ToList(); 

      var allTransReportProductsOption = new SelectListItem 
      { 
       Text = "All", 
       Value = String.Join(" | ", transanctionsReportProducts.Select(x => x.Text)) 
     }; 

      transanctionsReportProducts.Insert(0, allTransReportProductsOption); 

      return transanctionsReportProducts; 
     } 
+1

可以試試這個? 'transanctionsReportProducts = transanctionsReportProducts.OrderBy(el => el.Text);' – techspider

回答

4

你是不是做對OrderBy的任何回報。您可以立即返回OrderBy的結果。當你打電話給OrderBy時,它不會改變你傳入的現有列表。它會創建一個新的有序元素列表,並且你沒有做任何事情。

的更多信息,可以發現here

public IEnumerable<SelectListItem> createProductsDropdownForTransReport() 
{ 
    var resultsOfProductsSearch = findAllByEnumSet(
     EnumLookup.EnumSetType.SterlingWestProducts); 

    var transanctionsReportProducts = resultsOfProductsSearch 
     .Where(el => el.Ordinal != 95 && el.Ordinal != 253) 
     .Select(el => new SelectListItem { Text = el.Text, Value = el.Text }) 
     .OrderBy(el => el.Text) 
     .ToList(); 

    transanctionsReportProducts.Add(new SelectListItem { 
     Text = "SSN Trace", Value = "SSN Trace" }); 

    var allTransReportProductsOption = new SelectListItem 
    { 
     Text = "All", 
     Value = String.Join(" | ", transanctionsReportProducts.Select(x => x.Text)) 
    }; 

    transanctionsReportProducts.Insert(0, allTransReportProductsOption); 

    return transanctionsReportProducts.OrderBy(el => el.Text); 
} 
+0

你是說改變我的return語句來反映你的代碼片段嗎? –

+0

刪除您現有的退貨並在訂單中添加退貨OrderBy –

+0

我現在就試試。謝謝!! –

相關問題