9

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

DavidRR
  • 18,291
  • 25
  • 109
  • 191
amiry jd
  • 27,021
  • 30
  • 116
  • 215
  • 1
    Take a look at this question: http://stackoverflow.com/questions/4156949/ef4-linq-ordering-parent-and-all-child-collections-with-eager-loading-include. What you are trying to do is called "eager loading", and apparently, you you can't use `OrderBy` with `Include`. – devuxer Sep 23 '11 at 01:41
  • Yes, I know about eager loading, and if you look at the first code (created by my own) you'll see I use a foreach statement on each object in each level (top for child, and, child for grandchild) same as what explained on your prepared link. but this need more queries against the database! I'm searching a way to do this all in one query, not more! – amiry jd Sep 23 '11 at 02:11
  • Can you eager load your entire structure, then do ordering in your views when they are required? There's almost no reason why you should be leaking presentation logic (ordering) in your data access logic. – Daryl Teo Sep 23 '11 at 02:21
  • There is just one reason: avoiding several loop. Here is a lot of `foreach` loop that I want to avoid them. In querying against the db or in view, there is no deference between them, we'll have: `foreach(foreach())`. So sorry I can't explain more ): – amiry jd Sep 23 '11 at 03:25
  • But I think in final, I have to do this. Select all of them by `Include(Select())` and then order them. Thanks again, to your attention. – amiry jd Sep 23 '11 at 03:27

2 Answers2

27

Unfortunately eager loading (Include) doesn't support any filtering or sorting of loaded child collections. There are three options to achieve what you want:

  • Multiple roundtrips to the database with explicite sorted loading. That's the first code snippet in your question. Be aware that multiple roundtrips are not necessarily bad and that Include and nested Include can lead to huge multiplication of transfered data between database and client.

  • Use eager loading with Include or Include(....Select(....)) and sort the data in memory after they are loaded:

    var model2 = DbContext.SitePages
        .Where(p => p.ParentId == null && p.Level == 1)
        .OrderBy(p => p.Order)
        .Include(p => p.Children.Select(c => c.Children))
        .ToList();
    
    foreach (var parent in model2)
    {
        parent.Children = parent.Children.OrderBy(c => c.Order).ToList();
        foreach (var child in parent.Children)
            child.Children = child.Children.OrderBy(cc => cc.Order).ToList();
    }
    
  • Use a projection:

    var model2 = DbContext.SitePages
        .Where(p => p.ParentId == null && p.Level == 1)
        .OrderBy(p => p.Order)
        .Select(p => new
        {
            Parent = p,
            Children = p.Children.OrderBy(c => c.Order)
                .Select(c => new
                {
                    Child = c,
                    Children = c.Children.OrderBy(cc => cc.Order)
                })
        })
        .ToList() // don't remove that!
        .Select(a => a.Parent)
        .ToList();
    

This is only a single roundtrip and works if you don't disable change tracking (don't use .AsNoTracking() in this query). All objects in this projection must be loaded into the context (the reason why the first ToList() is necessary) and the context will tie the navigation properties correctly together (which is a feature called "Relationship span").

Community
  • 1
  • 1
Slauma
  • 175,098
  • 59
  • 401
  • 420
  • OMG special thanks Slauma! Good done well job :D:D:D That works, I trace it and JUST ONE query sent against db (^_^) special thanks dear. I just changed the inner `Select` with `.Select(c => new { Parent = c, Children = c.Children. OrderBy(cc => cc.Order) })`. Thanks again and again again. – amiry jd Sep 23 '11 at 12:09
  • 1
    @ChrisMoschini: Are you sure that `ProxyCreationEnabled` matters? I almost never use change tracking proxies and relationship span still works. – Slauma Jul 14 '14 at 23:47
  • @Slauma You're right! We have code that uses both Proxy disabled and no tracking for fast read-only queries that fails to do this kind of projection; but Proxy disabled is just fine, it's just the AsNoTracking() that throws a wrench in. – Chris Moschini Jul 15 '14 at 00:16
  • One other thing as long as I'm testing this though - ordering child lists seems to query much faster from SQL Server for long lists, than leaving order undefined, even if you're fine with unordered lists as the final outcome. – Chris Moschini Jul 15 '14 at 00:30
  • Just in case this doesn't work for anyone. Make sure you aren't using the dbcontext Load() method anywhere. I was going round and round trying to figure out why this was upvoted so much when it doesn't work, but once I removed that bingo! – flux Jan 07 '16 at 15:55
  • 1
    **UPD info**: since EF Core 5.0 - Filteing and Ordering are supported [Filtered include](https://learn.microsoft.com/en-us/ef/core/querying/related-data/eager#filtered-include) – WhiteKnight Dec 24 '20 at 11:07
0

Have you try following?

...
Select(from ch in c.children
       orderby ch.property desending
       select ch)
...
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Doesn't work! The error is *The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path* – amiry jd Sep 23 '11 at 08:31