简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM