How to convert ListItemCollection
(DropDownList.items
) to a Dictionary<String, String>
(I know it can be done through for each loop) is there any other way linq?
You can use LINQ:
collection.Cast<ListItem>().ToDictionary(i => i.Value, i => i.Text);
It's not immediately known what the type of the item is, hence the cast method (at least intellisense didn't bring it up for me). But ToDictionary() should get you there, and specify whatever you want as the key and the value.
HTH.
See this question:
var result =
dropdownlist.items.Cast<ListItem>()
.ToDictionary(item => item.Value,
item => item.Text,
StringComparer.OrdinalIgnoreCase);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With