实体框架排序导航对象

发布于 2025-01-08 08:16:37 字数 521 浏览 4 评论 0原文

我有一个如下所示的查询

 return this.Context.Modules
               .Select(p => new
               {
                   Module = p,
                   Page = p.Pages.OrderBy(c => c.AuthOrder)

               }).ToList()
               .Select(a => a.Module)
               .ToList();

,但我正在使用 EF 4.2,我尝试像这样并通过 SQL Profiler 进行观察,EF 生成更多嵌套的 sql 查询,我只想对这项作业进行一个查询,这样

select * from Modules m join Pages p on m.ID = p.Module_ID
order by p.AuthOrder

怎么可能?

i have a query like below

 return this.Context.Modules
               .Select(p => new
               {
                   Module = p,
                   Page = p.Pages.OrderBy(c => c.AuthOrder)

               }).ToList()
               .Select(a => a.Module)
               .ToList();

but i am using EF 4.2 and i try like this and watching by SQL Profiler,EF generate much more sql queries its nested,i want to only one query for this job like this

select * from Modules m join Pages p on m.ID = p.Module_ID
order by p.AuthOrder

how is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

提笔落墨 2025-01-15 08:16:37

这样的查询

select * from Modules m join Pages p on m.ID = p.Module_ID
order by p.AuthOrder

可以通过以下方式实现:

var q = (from c in Context.Modules
            join o in Context.Pages on c.ID equals o.Module_ID
            orderby o.AuthOrder
            select new {c, o}).ToList();

Query like that

select * from Modules m join Pages p on m.ID = p.Module_ID
order by p.AuthOrder

can be implemented next way:

var q = (from c in Context.Modules
            join o in Context.Pages on c.ID equals o.Module_ID
            orderby o.AuthOrder
            select new {c, o}).ToList();
一场春暖 2025-01-15 08:16:37

您可能正在寻找 Include()指定要包含在结果中的相关对象(即必须在 SQL 请求中连接哪些表):

return this.Context.Modules.Include("Pages")
               .Select(p => new
               {
                   Module = p,
                   Page = p.Pages.OrderBy(c => c.AuthOrder)

               }).ToList()
               .Select(a => a.Module)
               .ToList();

编辑
您确定您的查询有意义吗?您实际上只选择了 Module,那么为什么要尝试先订购 Pages 呢?

You probably are looking for Include() that specifies related object to include in your result (i.e. which tables must be joined in your SQL request):

return this.Context.Modules.Include("Pages")
               .Select(p => new
               {
                   Module = p,
                   Page = p.Pages.OrderBy(c => c.AuthOrder)

               }).ToList()
               .Select(a => a.Module)
               .ToList();

Edit:
Are you sure your query makes sense? You're actually only selecting Module, so why are you trying to order Pages first?

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