Reputation: 196539
I have an asp.net-mvc website and i am trying to use a dual listbox plugin
I have an array of
IEnumerable<SelectListItem> allItems:
and I have an array of Ints
IEnumerable<int> selectedIds;
that represents the Selected Value that someone filtered by. My goal is to see if given these two imputs, I can create two IEnumerable
IEnumerable<SelectListItem> selectedItems;
IEnumerable<SelectListItem> nonSelectedItems;
that I would use to populate the dual listbox plugin. I can get the selectedItems pretty easily but when i try to create the nonselected List i try to use Except() but it doesn't seem to be able to take the full list and "subtract" any item in the selected list.
Am I using the wrong method to do this filter?
Upvotes: 0
Views: 1550
Reputation: 887451
You're looking for Where()
:
allItems.Where(o => !selectedIds.Contains(int.Parse(o.Value)))
You can compute both sublists at once using ToLookup
:
var lookup = allItems.ToLookup(o => selectedIds.Contains(int.Parse(o.Value)));
var selectedItems = lookup[true];
You can make these much faster by changing the IEnumerable<int>
to a HashSet<int>
so that Contains()
becomes O(1). Just make sure not to covarinatly lose that.
Upvotes: 2