Build a Where clause using a loop and concatening each iterarion with an OR

Viewed 1105

I have a list of N pairs of integers, e.g.:

2, 4
5, 7
9, 10
11, 12

And I need to build a query like:

WHERE 
    (foo = 2 AND bar = 4) OR
    (foo = 5 AND bar = 7) OR
    (foo = 9 AND bar = 10) OR
    (foo = 11 AND bar = 12)

If it was a constant length list, I could write something like:

var query = myClass.Where(x =>
    (foo == values[0][0] && bar == values[0][1]) ||
    (foo == values[1][0] && bar == values[1][1]) ||
    (foo == values[2][0] && bar == values[2][1]) ||
    (foo == values[3][0] && bar == values[3][1]));

But the length of the list varies, and I am looking for a way to create the query using a loop.

I found I can use Queryable.Union() for a similar result, but considering there are more conditions in the query, and the list of pairs can be long, I would prefer to avoid the union.

Is there any solution for this problem?

5 Answers
Related