Dean Friedland Dean Friedland - 3 months ago 12
ASP.NET (C#) Question

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

lll lll
Answer

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