locked
.Orderby() in tri-level .Include() Entity Framework Linq query RRS feed

  • Question

  • I have 3 entities that all encompass eachother. Make can have many models and models can have many years. The data I am retrieving looks like this: 

    So far I can only order by Makes and not models or years. 

    var data = context.MakeSources.OrderBy(P => P.Name).Include(A => A.ModelSources
                        .Select(B => B.YearSources));
    Placing more OrderBy calls within the include throws errors. How can I get a neatly ordered collection of first Makes ordered by asc, for each make its models ordered by ascending and for each model its years ordered by ascending?

    • Moved by CoolDadTx Wednesday, August 6, 2014 5:48 PM EF related
    Wednesday, August 6, 2014 5:13 PM

Answers

  • Hello,

    You could try with LINQ syntax Orderby as below, I do not have your current model, I use one to many relationship model as the example:

    var result = (from o in db.Orders
    
                                  join od in db.OrderDetails on o.OrderID equals od.OrderID
    
                                  orderby od.OrderDetailName,o.OrderName 
    
                                  select new { OrderName = o.OrderName, OrderDetailName = od.OrderDetailName }).ToList();
    

    If you want know more about the OrderBy in LINQ, refer to this link:

    http://www.codeproject.com/Articles/231162/Ordering-data-in-LINQ-Queries-by-more-than-one-col

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Monday, August 18, 2014 3:27 AM
    Tuesday, August 12, 2014 9:43 AM

All replies

  • After the first "OrderBY", add "ThenBy". For instance:

    var data = context.MakeSources.Include(A => A.ModelSources
                       
    .Select(B => B.YearSources)).OrderBy(P => P.Name).ThenBy(p=>p.ModelSource.Name);

    Wednesday, August 6, 2014 5:35 PM
  • If I try that intellisense does not allow me to select the "Name" property of "ModelSource". It gives me a list of extension methods to be used on IQueryables instead.

    

    Wednesday, August 6, 2014 6:08 PM
  • Hello,

    You could try with LINQ syntax Orderby as below, I do not have your current model, I use one to many relationship model as the example:

    var result = (from o in db.Orders
    
                                  join od in db.OrderDetails on o.OrderID equals od.OrderID
    
                                  orderby od.OrderDetailName,o.OrderName 
    
                                  select new { OrderName = o.OrderName, OrderDetailName = od.OrderDetailName }).ToList();
    

    If you want know more about the OrderBy in LINQ, refer to this link:

    http://www.codeproject.com/Articles/231162/Ordering-data-in-LINQ-Queries-by-more-than-one-col

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Monday, August 18, 2014 3:27 AM
    Tuesday, August 12, 2014 9:43 AM