lambda expression for left join

.net c# entity-framework lambda

Question

I have tableA and tableB.
I would like to perform left join using lambda expression. This is the equal sql statement:

SELECT *
FROM tableA A
    LEFT JOIN tableB B ON A.userId=B.userId

How can I do that using lambda expression?

1
4
7/12/2011 2:23:12 PM

Accepted Answer

It's usually an error to use an explicit join in LINQ to Entities.

Instead, use the navigation properties:

var q = Context.TableAs.Select(a => new { a.Foo, a.TableB.Bar });

LINQ to Entities will coalesce null references. So if a.TableB is null for some record in TableAs, then a.TableB.Bar will return null instead of giving you a null reference exception. So it behaves like a SQL LEFT JOIN

5
4/17/2013 2:22:10 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow