Select multiple where in one 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 classes containing about 16000 elements

When it checkedListBoxHoofdbrandstof.CheckedItems

contains more than one item, the query only uses the results from the last where-where clause.

For example: I have 2 values, A and B, and even though A returns 100 rows and B returns 50 rows, the result only includes the last 50 rows. A is no longer included in the results.

I tried to use a.Hoofdbrandstof.Any

but it results in an error about types. I have also tried a.Hoofdbrandstof.Equals

with the same results.

Does anyone know how I can combine these results so that both results from A and B are filtered by var?

+3


source to share


1 answer


Simple way:

var checkedItems = checkedListBoxHoofdbrandstof.CheckedItems;
var filtered = from a in lijst
               where checkedItems.Contains(a.Hoofdbrandstof)
               select a

      



But the complexity of this method if O(n^2)

, to reduce it to O(n)

, uses the union 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

      

+2


source







All Articles