Sorting a ListBox ListItems with LINQ?
c#, linq
Solution
May be first you should store the list in generic collection and then sort it. Something like this:
List<ListItem> list = new List<ListItem>(lbxCustomers.Items.Cast<ListItem>());
list = list.OrderBy(li => li.Text).ToList<ListItem>();
lbxCustomers.Items.Clear();
lbxCustomers.Items.AddRange(list.ToArray<ListItem>());
Problem
I'm trying to sort the items in a ListBox. However, upon doing so the Item Value gets set to the Item Text. Any help would be appreciated. ``` lbxCustomers.DataSource = lbxCustomers.Items.Cast<ListItem>().Reverse().ToList(); lbxCustomers.DataBind(); ```