这个EF Join方法调用出了什么问题?

dud*_*er4 6 c# linq entity-framework-4

我目前只限于在EF4数据存储库上使用扩展方法; 我不能使用linq来EF.我正在努力做一个简单的3表连接工作.这是代码:

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 );  
Run Code Online (Sandbox Code Playgroud)

这是(基本上)生成的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 )  
Run Code Online (Sandbox Code Playgroud)

我可以在代码中更改什么来消除使外部联接无效的查询意图?

Rup*_*Rup 2

我不清楚你到底想返回什么——孙子的任何财产?你的第二个连接返回 GrandChild 对象,( c, g ) => g所以我认为你只需要

.Select( joined => joined.Whatever );   
Run Code Online (Sandbox Code Playgroud)

因为这里joined是GrandChild对象。