简体   繁体   English

将项目添加到列表中,但不会按字母顺序排序

[英]Added item to a list but it will not sort alphabetically

The below code is used to create a dropdown list of services that my company offers. 以下代码用于创建我公司提供的服务的下拉列表。 The services are being pulled from our database and I hard coded and added an additional item named "SSN Trace" to the list. 这些服务正在从我们的数据库中提取,我进行了硬编码,并在列表中添加了一个名为“ SSN Trace”的附加项。 The problem is that the item is still showing up at the end of the list instead of falling in alphabetical order with the rest of the list items. 问题在于该项目仍显示在列表的末尾,而不是按照字母顺序与其余列表项一起出现。 Can anyone help? 有人可以帮忙吗?

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;
        }

CORRECT CODE: 正确的代码:

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;
        }

You are not doing anything on the return by OrderBy . 您在OrderBy退货时不做任何事情。 You can immediately return the result of the OrderBy . 您可以立即返回OrderBy的结果。 When you call OrderBy , it doesn't mutate the existing list that you passed in. It creates a new list of the ordered elements and you are not doing anything on it. 当您调用OrderBy ,它不会使传入的现有列表发生突变。它会创建一个新的有序元素列表,并且您不会对其执行任何操作。

More information can be found 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM