Im using LINQ to retrive cheched Items from CheckBoxList control:
Here the LINQ :
IEnumerable<int> allChecked = (from ListItem item in CheckBoxList1.Items
where item.Selected
select int.Parse(item.Value));
My question is why item have to be ListItem type?
The CheckedListBox was created before generics were introduced, thus the Items collection returns objects rather than the strongly typed ListItem. Luckily, fixing this with LINQ is relatively easy using the OfType() (or Cast) methods:
IEnumerable<int> allChecked = (from ListItem item in CheckBoxList1.Items.OfType<ListItem>()
where item.Selected
select int.Parse(item.Value));
My question is why item have to be ListItem type?
Because CheckBoxList1.Items
is an ObjectCollection whose members are ListItem
s. That's what your linq query is working against.
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