Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Join on multiple fields

What would be the equivalent of the following T-SQL query in L2E using Lambda expressions?

Select * from a INNER JOIN b on a.Foo = b.Foo OR a.Foo = b.Bar  

I want to join a and b when a.Foo equal to b.Foo OR b.Bar

Thanks.

like image 785
Kamyar Avatar asked Dec 14 '11 14:12

Kamyar


1 Answers

You can't do an "or" style join in LINQ with an actual join clause. All join clauses in LINQ are equijoins. The closest you can come is a where clause:

var query = from a in A
            from b in B
            where a.Foo == b.Foo || a.Foo == b.Bar
            select new { a, b };
like image 80
Jon Skeet Avatar answered Sep 19 '22 20:09

Jon Skeet