简体   繁体   中英

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. 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 . You can immediately return the result of the 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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