LINQ查询按复选框列表中的选定项筛选

时间:2011-06-30 20:50:44

标签: vb.net linq linq-to-objects checkboxlist

无法通过Google或SO问题找到这个......

我的表单上有一个复选框列表框。我想通过列表框中选中的ID列表来过滤我的列表,在SQL中我会这样做,如“Where TypeId In(1,4,5,7)”...我该怎么做LINQ?

我觉得我错过了一个非常明显的答案,但却无法得到答案。

为了论证...这是我对样本数据所拥有的:

In Colors (List<of currentColors>)
ID, Name, TypeId
1, Red, 1
2, Blue, 1
3, Green, 2
4, Pink, 3

CheckboxList中的选定类型2和3:filteredColors

filteredResults = (From C In WorkItemMonitor Where ????).ToList()

filteredResults中的预期项目将是: [3,格林,2],[4,粉红色,3]

编辑: 我的当前查询..(抱歉被告知这将是一个列表,原来是我正在过滤的数据表)

Dim workItemsListing As DataTable
workItemsListing = (From L In WorkItemMonitor.AsEnumerable() _
Where clbStatus.CheckedItems.Contains(L.Item("CurrentStatusId"))).CopyToDataTable()

1 个答案:

答案 0 :(得分:3)

List<CurrentColor> colors = chkListCurrentColors.CheckedItems.Cast<CurrentColor> ();
filteredResults = (From C In WorkItemMonitor colors.Contains(C.TypeId)).ToList()

这就是我能用你的描述做的最好的事情。如果您需要更多帮助,则需要显示您添加到CheckedListBox的内容以及颜色类型。

相关问题