将项添加到列表但不按字母顺序排序

时间:2016-08-08 21:27:07

标签: c# asp.net .net

以下代码用于创建我公司提供的服务下拉列表。这些服务正从我们的数据库中提取出来,我进行了硬编码,并在列表中添加了一个名为“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 个答案:

答案 0 :(得分:4)

您在df1 = df['info'].str[1:-1].str.replace(' ', '').str.get_dummies(',') df1.rename(columns=lambda x: x.rsplit(':')[-1], inplace=True) df2 = pd.concat([df, df1.astype(int)], axis=1) df2 Out: info Price Sailing Boating Surfing 0 [100:Sailing] $100 1 0 0 1 [150:Boating, 100:Sailing] $200 1 1 0 2 [200:Surfing] $300 0 0 1 返回时没有做任何事情。您可以立即返回OrderBy的结果。当您调用OrderBy时,它不会改变您传入的现有列表。它会创建一个新的有序元素列表,而您没有对其执行任何操作。

可以找到更多信息here

OrderBy