LINQ to CheckBoxList 数据项
我使用 LINQ 从 CheckBoxList 控件中检索已检查的项目:
这里是 LINQ:
IEnumerable<int> allChecked = (from ListItem item in CheckBoxList1.Items
where item.Selected
select int.Parse(item.Value));
我的问题是为什么项目必须是 ListItem 类型?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CheckedListBox 是在引入泛型之前创建的,因此 Items 集合返回对象而不是强类型 ListItem。幸运的是,使用 OfType()(或 Cast)方法通过 LINQ 修复此问题相对容易:
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:
因为
CheckBoxList1.Items
是一个 ObjectCollection,其成员是ListItem
。这就是您的 linq 查询所针对的问题。Because
CheckBoxList1.Items
is an ObjectCollection whose members areListItem
s. That's what your linq query is working against.我认为因为
CheckBoxList
继承自ListControl
并且Items
属性继承自ListControl
I think because
CheckBoxList
inherits fromListControl
andItems
property is inherited fromListControl