Entity Framework, MVC 3, OrderBy in LINQ To Entities

前端 未结 2 596
时光取名叫无心
时光取名叫无心 2020-12-21 02:45

I\'ve got the following query:

model.Page = db.Pages
    .Where(p => p.PageId == Id)
    .Include(p => p.Series
                   .Select(c => c.Co         


        
相关标签:
2条回答
  • 2020-12-21 03:34

    Off the top of my head, untested:

    model.Page = db.Pages
                   .Where(p => p.PageId == Id)
                   .Include(p => p.Series
                                  .Select(c => c.Comics
                                                .Select(col => col.Collection)
                                                .OrderBy(o => o.ReadingOrder)))
                   .SingleOrDefault();
    
    0 讨论(0)
  • 2020-12-21 03:38

    The exception "...Include path expression must refer to a navigation property..." basically complains that c.Comics.OrderBy is not a navigation property. (It's a legitimate complaint, I think.)

    Actually it's not supported by EF to apply sorting (and also filtering) in eager loading statements (Include).

    So, what can you do?

    Option 1:

    Sort in memory after you have loaded the entity:

    model.Page = db.Pages
        .Where(p => p.PageId == Id)
        .Include(p => p.Series.Select(c => c.Comics
                              .Select(col => col.Collection)))
        .SingleOrDefault();
    
    if (model.Page != null)
    {
        foreach (var series in model.Page.Series)
            series.Comics = series.Comics.OrderBy(c => c.ReadingOrder).ToList();
    }
    

    Ugly, but because you are loading apparently only a single Page object by id it's possibly faster (LINQ to Objects in memory) than the following options (if Series and Comics collections are not extraordinarily long).

    Option 2:

    Break down the query in parts which mix eager and explicite loading:

    model.Page = db.Pages
        .Where(p => p.PageId == Id)
        .Include(p => p.Series) // only Series collection is included
        .SingleOrDefault();
    
    if (model.Page != null)
    {
        foreach (var series in model.Page.Series)
            db.Entry(series).Collection(s => s.Comics).Query()
              .Include(c => c.Collection)
              .OrderBy(c => c.ReadingOrder)
              .Load(); // one new DB query for each series in loop
    }
    

    Option 3:

    Projection?

    Here and here is by the way something about the dangers of complex Include chains of multiple navigation properties. It can load huge amounts of duplicated data. Include ensures that you only have one DB roundtrip but possibly at the cost of much more transfered data. Explicite loading has multiple roundtrips but with possibly less data in total.

    (I know, I gave you this Include...Select...Select...Select... chain, but how could I know that you would take me serious :). Well, depending on the size of your nested collections it can still be the best option.)

    0 讨论(0)
提交回复
热议问题