这个 EF Join 方法调用有什么问题?
我目前仅限于在 EF4 数据存储库上使用扩展方法;我无法使用 linq to 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 );
这是(本质上)生成的 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 )
我可以在代码中更改什么来消除使查询意图无效的左外连接?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不清楚你到底想返回什么——孙子的任何财产?您的第二个连接返回 GrandChild 对象
( c, g ) => g
所以我认为你只需要因为这里
joined
是 GrandChild 对象。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 needbecause here
joined
is the GrandChild object.