lambda expression for left join

.net c# entity-framework lambda

Question

Tables A and B are available.
I'd want to use a lambda expression to make a left join. The equal SQL statement is as follows:

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

How would I go about doing it using lambda expression?

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

Accepted Answer

Typically, it's wrong to utilize an explicitjoin to Entities in LINQ.

Use the navigation properties instead:

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

Null references will be combined by LINQ to Entities. So ifa.TableB is null to some degree inTableAs , thena.TableB.Bar will be backnull rather than throwing a null reference error. It thus acts as 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