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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With