I have a linq to sql expression and I want to OR some expressions in it.
I have two tables called A
and B
, and I have a relation in a table called AB
with the fields A_Id
, B_Id
and a number called State
.
User can create a request with some pairs of B
s with specific State
s. And I should return all A
s that match these pairs.
Example:
A_Id | B_Id | State
-------------------
1 | 101 | 1001
2 | 102 | 1002
3 | 103 | 1003
3 | 101 | 1003
and the request has these pairs:
B_Id: 101 and State: 1001
OR
B_Id: 103 and State: 1003
and the number of pairs could be more.
I have this query for only one pair of B
and State
:
int b_Id = 101;
int state = 1001;
var query = dbContext.As.Where(a => dbContext.ABs.Any(ab => ab.A_Id == a.Id && ab.B_Id == b_Id && ab.State == state);
If I have more pairs I should OR them like this:
... (ab.B_Id == b_Id[0] && ab.State == state[0]) || (ab.B_Id == b_Id[1] && ab.State == state[1]) || ...
But I may have more pairs and I coudn't handle it like that.
How can I have something like OR
that could pass more pairs to get complete result?