使用CheckedListBoxItemCollection.AsQueryable().Provider.CreateQuery查询winforms控件(
我需要查询一个winforms控件(CheckedListBoxControl),该控件具有一个CheckedListBoxItemCollection,我想查询该控件中CheckedListBoxItem的属性“值”内的某个Id使用CheckedListBoxItemCollection.AsQueryable().Provider.CreateQuery查询winforms控件(,winforms,controls,where,asqueryable,Winforms,Controls,Where,Asqueryable,我需要查询一个winforms控件(CheckedListBoxControl),该控件具有一个CheckedListBoxItemCollection,我想查询该控件中CheckedListBoxItem的属性“值”内的某个Id CheckedListBoxItemCollection items = myCheckedListBoxControl.Items; foreach(Department dep in departmentList) { bool isDepExisting
CheckedListBoxItemCollection items = myCheckedListBoxControl.Items;
foreach(Department dep in departmentList)
{
bool isDepExisting = items.AsQueryable().Where( the .Where clause does not exist );
// How can I query for the current dep.Id in the departmentList and compare this dep.Id with every Item.Value in the CheckedListBoxControl and return a bool from the result ???
if(!isDepExisting)
myCheckedListBoxControl.Items.Add( new CheckedListBoxItem(dep.id);
}
更新:
IEnumberable<CheckedListBoxItem> checks = items.Cast<CheckedListBoxItem>().Where(item => item.Value.Equals(dep.InternalId));
IEnumberable checks=items.Cast().Where(item=>item.Value.Equals(dep.InternalId));
为什么Visual Studio说找不到IEnumerable或它的IEnumerable命名空间?当我使用“var”时,我就可以编译我的代码。但是我公司的老板禁止我使用var…CheckListBox.Items只实现IEnumerable,而不是
IEnumerable
。你会得到AsQueryable()的重载它返回IQueryable(不是泛型的),它只有Cast和OfType扩展方法
将项目从对象转换回部门。如下所示:
var q = checkedListBox1.Items.Cast<Department>().AsQueryable().Where((d) => d.Id == 1);
var q=checkedListBox1.Items.Cast().AsQueryable(),其中((d)=>d.Id==1);
顺便说一句,您不再需要AsQueryable()。您误解了一件事:CheckListBoxControl没有数据绑定到部门实体。CheckedListBoxControl从XML文件手动添加其项。“对象”在departmentList和CheckedListBoxControl中没有所有相同的属性。唯一相同的属性是我要搜索的Id。但可能我也误解了你。我用一个代码示例和一个相关问题更新了我的init帖子:嗯,我不得不在没有完整代码示例的情况下进行一些猜测。听起来像我的answer对你很有用。你为什么不这样标记呢?因为我仍然想等待更新问题的最终答案,否则没问题,Hans!请取消标记答案,这样我就可以删除它。提供有用的答案对大多数对此做出贡献的人来说都很重要。现在我感到困惑。你的答案对我来说没问题。更新的问题不是b说到起源问题。回答它只是冰上的奶油……但这并不取决于接受整个问题作为解决方案。因此,当你不回答它时,你仍然得到了解决方案。