简体   繁体   中英

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

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):

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM