Skip to main content
Removed comment pointing to what appeared to be a missing parenthesis. A big mistake on the part of this editor.
Source Link
DavidRR
  • 19.8k
  • 27
  • 114
  • 202

This is the second step of a question explained here: EF 4.1 code-first: How to load related data (parent-child-grandchild)?. With @Slauma's guidance, I have successfully retrieved data with this approach:

var model = DbContext.SitePages .Where(p => p.ParentId == null && p.Level == 1) .OrderBy(p => p.Order) // ordering parent .ToList(); foreach (var child in model) { // loading children DbContext.Entry(child) .Collection(t => t.Children) .Query() .OrderBy(t => t.Order) // ordering children .Load(); foreach (var grand in child.Children) { // loading grandchildren DbContext.Entry(grand) .Collection(t => t.Children) .Query() .OrderBy(t => t.Order) // ordering grandchildren .Load(); } } 

Though this approach works, it sends many queries to the database and I am searching for a way to do this all in just one query. With @Slauma's guidance (explained in the answer at the above link), I have changed the query to this one:

var model2 = DbContext.SitePages .Where(p => p.ParentId == null && p.Level == 1) .OrderBy(p => p.Order) .Include(p => p.Children // Children: how to order theme??? <<== MISSING ENDING PARENTHESIS HERE? .Select(c => c.Children) // Grandchildren: how to order them??? ).ToList(); 

Now, how can I order children (and grandchildren) when selecting them (such as shown in the first code example above)?

This is the second step of a question explained here: EF 4.1 code-first: How to load related data (parent-child-grandchild)?. With @Slauma's guidance, I have successfully retrieved data with this approach:

var model = DbContext.SitePages .Where(p => p.ParentId == null && p.Level == 1) .OrderBy(p => p.Order) // ordering parent .ToList(); foreach (var child in model) { // loading children DbContext.Entry(child) .Collection(t => t.Children) .Query() .OrderBy(t => t.Order) // ordering children .Load(); foreach (var grand in child.Children) { // loading grandchildren DbContext.Entry(grand) .Collection(t => t.Children) .Query() .OrderBy(t => t.Order) // ordering grandchildren .Load(); } } 

Though this approach works, it sends many queries to the database and I am searching for a way to do this all in just one query. With @Slauma's guidance (explained in the answer at the above link), I have changed the query to this one:

var model2 = DbContext.SitePages .Where(p => p.ParentId == null && p.Level == 1) .OrderBy(p => p.Order) .Include(p => p.Children // Children: how to order theme??? <<== MISSING ENDING PARENTHESIS HERE? .Select(c => c.Children) // Grandchildren: how to order them??? ).ToList(); 

Now, how can I order children (and grandchildren) when selecting them (such as shown in the first code example above)?

This is the second step of a question explained here: EF 4.1 code-first: How to load related data (parent-child-grandchild)?. With @Slauma's guidance, I have successfully retrieved data with this approach:

var model = DbContext.SitePages .Where(p => p.ParentId == null && p.Level == 1) .OrderBy(p => p.Order) // ordering parent .ToList(); foreach (var child in model) { // loading children DbContext.Entry(child) .Collection(t => t.Children) .Query() .OrderBy(t => t.Order) // ordering children .Load(); foreach (var grand in child.Children) { // loading grandchildren DbContext.Entry(grand) .Collection(t => t.Children) .Query() .OrderBy(t => t.Order) // ordering grandchildren .Load(); } } 

Though this approach works, it sends many queries to the database and I am searching for a way to do this all in just one query. With @Slauma's guidance (explained in the answer at the above link), I have changed the query to this one:

var model2 = DbContext.SitePages .Where(p => p.ParentId == null && p.Level == 1) .OrderBy(p => p.Order) .Include(p => p.Children // Children: how to order theme??? .Select(c => c.Children) // Grandchildren: how to order them??? ).ToList(); 

Now, how can I order children (and grandchildren) when selecting them (such as shown in the first code example above)?

Copy-edited title and question and improved formatting of the code examples.
Source Link
DavidRR
  • 19.8k
  • 27
  • 114
  • 202

EF 4.1 code-first: How to order Order navigation properties when using Include and/or Select methods with EF 4.1 Code-First?

This is the second step of a question explained here: here (EF 4.1 code-first: How to load related data (parent-child-grandchild)? ). ByWith @Slauma's guidance, I could retrievehave successfully retrieved data. My first code was with this approach:

 var model = DbContext.SitePages .Where(p => p.ParentId == null && p.Level == 1) .OrderBy(p => p.Order) // ordering parent .ToList(); foreach (var child in model) { // loading children DbContext.Entry(child) .Collection(t => t.Children) .Query() .OrderBy(t => t.Order) // ordering children .Load(); foreach (var grand in child.Children) { // loading grandchildren DbContext.Entry(grand) .Collection(t => t.Children) .Query() .OrderBy(t => t.Order) // ordering grandchildren .Load(); } } 
var model = DbContext.SitePages .Where(p => p.ParentId == null && p.Level == 1) .OrderBy(p => p.Order) // ordering parent .ToList(); foreach (var child in model) { // loading children DbContext.Entry(child) .Collection(t => t.Children) .Query() .OrderBy(t => t.Order) // ordering children .Load(); foreach (var grand in child.Children) { // loading grandchildren DbContext.Entry(grand) .Collection(t => t.Children) .Query() .OrderBy(t => t.Order) // ordering grandchildren .Load(); } } 

ItThough this approach works, but, this will sendit sends many queries againstto the database and I wasam searching for a way to do this all in just one query. ByWith @Slauma's guidance (explained onin the answer at the above link), I changehave changed the query to this one:

 var model2 = DbContext.SitePages   .Where(p => p.ParentId == null && p.Level == 1)   .OrderBy(p => p.Order)   .Include(p => p.Children // Children: how to order theme???  <<== MISSING ENDING PARENTHESIS HERE?  .Select(c => c.Children) // Grandchildren: how to order them???   ).ToList(); 

Now, how can I order children (and grandchildren) when selecting them (such as shown in the first code example above)?

EF 4.1 code-first: How to order navigation properties when using Include and/or Select methods?

This is the second step of a question explained here (EF 4.1 code-first: How to load related data (parent-child-grandchild)? ). By @Slauma's guidance I could retrieve data. My first code was this:

 var model = DbContext.SitePages .Where(p => p.ParentId == null && p.Level == 1) .OrderBy(p => p.Order) // ordering parent .ToList(); foreach (var child in model) { // loading children DbContext.Entry(child) .Collection(t => t.Children) .Query() .OrderBy(t => t.Order) // ordering children .Load(); foreach (var grand in child.Children) { // loading grandchildren DbContext.Entry(grand) .Collection(t => t.Children) .Query() .OrderBy(t => t.Order) // ordering grandchildren .Load(); } } 

It works, but, this will send many queries against the database and I was searching a way to do this all in just one query. By @Slauma's guidance (explained on above link) I change the query to this one:

 var model2 = DbContext.SitePages   .Where(p => p.ParentId == null && p.Level == 1)   .OrderBy(p => p.Order)   .Include(p => p.Children // Children: how to order theme???  .Select(c => c.Children) // Grandchildren: how to order them???   ).ToList(); 

Now, how can I order children (and grandchildren) when selecting them (such as first code above)?

Order navigation properties when using Include and/or Select methods with EF 4.1 Code-First?

This is the second step of a question explained here: EF 4.1 code-first: How to load related data (parent-child-grandchild)?. With @Slauma's guidance, I have successfully retrieved data with this approach:

var model = DbContext.SitePages .Where(p => p.ParentId == null && p.Level == 1) .OrderBy(p => p.Order) // ordering parent .ToList(); foreach (var child in model) { // loading children DbContext.Entry(child) .Collection(t => t.Children) .Query() .OrderBy(t => t.Order) // ordering children .Load(); foreach (var grand in child.Children) { // loading grandchildren DbContext.Entry(grand) .Collection(t => t.Children) .Query() .OrderBy(t => t.Order) // ordering grandchildren .Load(); } } 

Though this approach works, it sends many queries to the database and I am searching for a way to do this all in just one query. With @Slauma's guidance (explained in the answer at the above link), I have changed the query to this one:

var model2 = DbContext.SitePages .Where(p => p.ParentId == null && p.Level == 1) .OrderBy(p => p.Order) .Include(p => p.Children // Children: how to order theme??? <<== MISSING ENDING PARENTHESIS HERE?  .Select(c => c.Children) // Grandchildren: how to order them??? ).ToList(); 

Now, how can I order children (and grandchildren) when selecting them (such as shown in the first code example above)?

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

This is the second step of a question explained here (EF 4.1 code-first: How to load related data (parent-child-grandchild)? )here (EF 4.1 code-first: How to load related data (parent-child-grandchild)? ). By @Slauma@Slauma's guidance I could retrieve data. My first code was this:

It works, but, this will send many queries against the database and I was searching a way to do this all in just one query. By @Slauma@Slauma's guidance (explained on above link) I change the query to this one:

This is the second step of a question explained here (EF 4.1 code-first: How to load related data (parent-child-grandchild)? ). By @Slauma's guidance I could retrieve data. My first code was this:

It works, but, this will send many queries against the database and I was searching a way to do this all in just one query. By @Slauma's guidance (explained on above link) I change the query to this one:

This is the second step of a question explained here (EF 4.1 code-first: How to load related data (parent-child-grandchild)? ). By @Slauma's guidance I could retrieve data. My first code was this:

It works, but, this will send many queries against the database and I was searching a way to do this all in just one query. By @Slauma's guidance (explained on above link) I change the query to this one:

complete the question
Source Link
amiry jd
  • 27.7k
  • 31
  • 120
  • 221
Loading
added 6 characters in body
Source Link
amiry jd
  • 27.7k
  • 31
  • 120
  • 221
Loading
Source Link
amiry jd
  • 27.7k
  • 31
  • 120
  • 221
Loading