I have a list with Article objects.
Parameters for this object are
Category, Description, Status, Class
The user can filter the list with the combinaison he wants. Only Description, or Category + Class, an so on.
So if the user choose a criterion i’ll get an int >= 0, otherwise i’ll get -1.
For example if he choose to filter with Status and Category, i’ll get
FilterCategory = x, FilterDescription = -1, FilterStatus = y, FilterClass = -1
My way to filter the list is the following:
if (FilterCategory != -1)
list = list.Where(a => a.Category == FilterCategory);
if (FilterDescription != -1)
list = list.Where(a => a.Description == FilterDescription);
if (FilterStatus != -1)
list = list.Where(a => a.Status == FilterStatus);
if (FilterClass != -1)
list = list.Where(a => a.Class == FilterClass);
In this way, i have to iterate the list 4 time, it’s not efficient with a lot of items. I would browse the list once, and check the 4 condition in an unique where. But i don’t know how to do that with the specific condition != -1.
Thank you
You can do a HUGE Where query moving all your conditions it a single
Whereby noticing that:is equivalent to:
Then the query is:
But I really doubt it will improve your enumeration performance.