Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select multiple where on same column

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 items

When 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?

like image 645
Jannick Bolten Avatar asked Jun 03 '26 04:06

Jannick Bolten


1 Answers

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
like image 197
Cédric Bignon Avatar answered Jun 05 '26 18:06

Cédric Bignon