left join using lambda expression
I have tableA and tableB.
I would like 开发者_StackOverflowto 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?
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
精彩评论