Given the following code:
var filtered = (from a in lijst select a);
foreach (string brandstof in checkedListBoxHoofdbrandstof.CheckedItems)
{
MessageBox.Show(brandstof);
filtered = (from a in lijst where a.Hoofdbrandstof.Contains(brandstof) select a);
}
MessageBox.Show(filtered.Count().ToString());
lijst is a List of a class , with about 16000 itemsWhen checkedListBoxHoofdbrandstof.CheckedItems contains more than 1 item, the query only uses the results from the last where-clause.
For example: I have 2 values, A and B, and despite the fact that A returns 100 rows, and B returns 50 rows, only the last 50 rows are included as a result. A is not included in the results anymore.
I tried using a.Hoofdbrandstof.Any, but that results in an error about types. I also tried a.Hoofdbrandstof.Equals, with the same results.
Does anyone know how I can combine these results, so that both the results from A and B are in var filtered?
The simple way:
var checkedItems = checkedListBoxHoofdbrandstof.CheckedItems;
var filtered = from a in lijst
where checkedItems.Contains(a.Hoofdbrandstof)
select a
But complexity of this method if O(n^2) to reduce it to O(n) use a Join operation
var checkedItems = checkedListBoxHoofdbrandstof.CheckedItems.Cast<string>().ToList();
var filtered = from a in lijst
join checkedItem in checkedItems on a.Hoofdbrandstof equals checkedItem
select a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With