Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with this EF Join method call?

I'm currently restricted to using extension methods on an EF4 data repository; I can't use linq to EF. I'm trying to make a simple 3 table join work. Here is the code:

var query = _readOnlyRepository.All<Parent>()
            .Where( p => p.Something == "something" )
            .Join( _readOnlyRepository.All<Child>(), // Child entity
                p => p.ParentID,                     // Parent Key
                c => c.ChildId,                      // Child Key
                ( p, c ) => c )                      // Projection
            .Join( _readOnlyRepository.All<GrandChild>(),
                c => m.ChildID,
                g => g.GrandChildID,
                ( c, g ) => g )
            .Select( joined => joined.Child.Whatever );  

Here is (essentially) the generated SQL:

select c2.Whatever
from Parent p
inner join Child c on p.ParentId = c.ParentId
inner join GrandChild g on c.ChildId = g.ChildId
left outer join Child c2 on g.ChildId = c2.ChildId
where ( "something" = p.Something )  

What can I change in code to eliminate that left outer join that invalidates the intent of the query?

like image 801
dudeNumber4 Avatar asked Jan 03 '11 20:01

dudeNumber4


1 Answers

I'm not clear exactly what you're attempting to return - the Whatever property of the GrandChild? Your second join returns the GrandChild object ( c, g ) => g so I think you'd just need

.Select( joined => joined.Whatever );   

because here joined is the GrandChild object.

like image 174
Rup Avatar answered Sep 20 '22 23:09

Rup