LINQ to CheckBoxList 数据项

发布于 2024-12-26 19:06:10 字数 335 浏览 5 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断舍离 2025-01-02 19:06:10

CheckedListBox 是在引入泛型之前创建的,因此 Items 集合返回对象而不是强类型 ListItem。幸运的是,使用 OfType()(或 Cast)方法通过 LINQ 修复此问题相对容易:

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

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)); 
半山落雨半山空 2025-01-02 19:06:10

我的问题是为什么 item 必须是 ListItem 类型?

因为 CheckBoxList1.Items 是一个 ObjectCollection,其成员是 ListItem。这就是您的 linq 查询所针对的问题。

My question is why item have to be ListItem type?

Because CheckBoxList1.Items is an ObjectCollection whose members are ListItems. That's what your linq query is working against.

迷离° 2025-01-02 19:06:10

为什么 item 必须是 ListItem 类型?

我认为因为 CheckBoxList 继承自 ListControl 并且 Items 属性继承自 ListControl

why item have to be ListItem type?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文