c# - Added item to a list but it will not sort alphabetically -


the below code used create dropdown list of services company offers. services being pulled our database , hard coded , added additional item named "ssn trace" list. problem item still showing @ end of list instead of falling in alphabetical order rest of list items. can 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 not doing on return orderby. can return result of orderby. when call orderby, doesn't mutate existing list passed in. creates new list of ordered elements , not doing on it.

more information can 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); } 

Comments