LINQ to CheckBoxList Data Elements

Im using LINQ to retrieve strikethrough items from a CheckBoxList control:

Here's LINQ:

IEnumerable<int> allChecked = (from ListItem item in CheckBoxList1.Items where item.Selected select int.Parse(item.Value)); 

My question is why should the item be a ListItem type?

+4
source share
3 answers

CheckedListBox was created before the introduction of generics, so the Items collection returns objects, not a strongly typed ListItem. Fortunately, fixing this with LINQ is relatively easy using OfType () (or Cast) methods:

 IEnumerable<int> allChecked = (from ListItem item in CheckBoxList1.Items.OfType<ListItem>() where item.Selected select int.Parse(item.Value)); 
+4
source

My question is why should the item be a ListItem type?

Because CheckBoxList1.Items is an ObjectCollection of which ListItem s are ListItem . This is what your linq query works with.

+3
source

why should the item be of type ListItem?

I think because CheckBoxList inherits from ListControl and Items property inherits from ListControl

+1
source

Source: https://habr.com/ru/post/1391127/


All Articles