[英]Entity Framework Ordering Navigate Objects
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 但是我正在使用EF 4.2,我尝试这样并通过SQL Profiler进行监视,EF生成了更多嵌套的sql查询,我只想对此工作进行一次查询
select * from Modules m join Pages p on m.ID = p.Module_ID
order by p.AuthOrder
how is that possible? 那怎么可能?
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();
You probably are looking for Include()
that specifies related object to include in your result (ie which tables must be joined in your SQL request): 您可能正在寻找
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();
Edit : Are you sure your query makes sense? 编辑 :您确定您的查询有意义吗? You're actually only selecting
Module
, so why are you trying to order Pages
first? 实际上,您实际上只是选择
Module
,所以为什么要首先尝试订购Pages
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.