I have bound array as datasource for ListBox . Now i need to convert listbox.Items to string of array collection.
foreach (string s1 in listBoxPart.Items)
{
clist.Add(s1);
}
here clist is string list, so how can i add ListBox.items to clist?
On
If the array which is the datasource contains strings:
clist.AddRange(listBoxPart.Items.Cast<string>());
or if the DataSource is a String[] or List<string>:
clist.AddRange((Ilist<string>)listBoxPart.DataSource);
if this is actually ASP.NET:
clist.AddRange(listBoxPart.Items.Cast<ListItem>().Select(li => li.Text));
You can project any
stringcontained insideItemsusingOfType. This means that any element inside theObjectCollectionwhich is actually astring, would be selected: