以下代碼用於創建我公司提供的服務下拉列表。這些服務正在從我們的數據庫中提取,我硬編碼並在列表中添加了一個名爲「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;
}
可以試試這個? 'transanctionsReportProducts = transanctionsReportProducts.OrderBy(el => el.Text);' – techspider