Left join using lambda expression

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

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

How can I do this with a lambda expression?

+4
source share
1 answer

This is usually a mistake in using explicit join in LINQ to Entities.

Use navigation properties instead:

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

LINQ to Entities will bind null references. Therefore, 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 SQL LEFT JOIN

+5
source

All Articles