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

发布于 2024-10-10 04:16:39 字数 1031 浏览 1 评论 0原文

我目前仅限于在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那一片橙海, 2024-10-17 04:16:39

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

.Select( joined => joined.Whatever );   

因为这里 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 need

.Select( joined => joined.Whatever );   

because here joined is the GrandChild object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文