RiddlerDev
RiddlerDev

Reputation: 7439

LINQ Query for filter by Selected Items in Checkbox List

Could not find this through Google or in SO questions...

I have a checkbox listbox on my form. I want to filter my List by the list of selected Ids from that listbox that are checked, in SQL I would have done this like "Where TypeId In (1, 4, 5, 7)"... how do I do that in LINQ?

I feel like I am missing a really obvious answer, but cannot get it.

For argument sake... here is the what I have for sample data:

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

Selected Types 2 and 3 in CheckboxList: filteredColors

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

Expected Items in filteredResults would be: [3, Green, 2], [4, Pink, 3]

EDIT: My Current Query.. (sorry was told it would be an list, turns out to be a datatable I am filtering)

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

Upvotes: 1

Views: 2281

Answers (1)

agent-j
agent-j

Reputation: 27913

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

That's about the best I can do with your description. If you need more help, you'll need to show how what you add to the CheckedListBox and the Type of your colors.

Upvotes: 3

Related Questions