I need to create a statement in LINQ with 3 tables and OR condition.
My function receives an integer, lets call it intZ
. I have 3 tables: tableA
, tableB
and tableC
.
tableA
has columns int1
, int2
and intB
. intB
is related to tableB
.
problem: int1
or int2
of tableA
can be intZ
and it has to match with one tableC
record.
I need an OR condition, but I have no idea where to place it. Does it go in the where clause? Or in the equals clause?
At the moment, I know how to join 3 tables, but the condition is killing me.
What is the difference between the two ways to create statements in linq? Is there a performance impact?
edit: Okay, now I think it's more clear. intZ
has to be related with intC
from tableC
, and this number can be int1
or int2
of tableA
.
Just add it to a Where
. In Linq2Sql this will be translated to an inner join (with or) on tableB
from a in tableA
from b in tableB.Where(x => x.A == a.A || x.B == a.B)
select new { a, b };
You can't use an "or" condition in joins in LINQ, as it only supports equijoins. But you should be able to do it in a where
clause with no problems. For example:
var query = from rowC in tableC
where rowC.intC == intZ
from rowA in tableA
where rowA.int1 == rowC.intC || rowA.int2 == rowC.intC
join rowB in tableB on rowA.intB equals rowB.intB
select new { rowA, rowB, rowC };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With