实体框架,MVC 3,LINQ to Entities中的OrderBy [英] Entity Framework, MVC 3, OrderBy in LINQ To Entities

查看:299
本文介绍了实体框架,MVC 3,LINQ to Entities中的OrderBy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询:

  model.Page = db.Pages 
.Where(p = p.PageId == Id)
.Include(p => p.Series
.Select(c => c.Comics
.Select(col => col .Collection)))
.SingleOrDefault();

尽管我现在需要通过名为ReadingOrder的属性订购漫画, / p>

我尝试过:

  model.Page = db.Pages 
.Where(p => p.PageId == Id)
.Include(p => p.Series.Select(c => c.Comics.OrderBy(o => o .ReadingOrder)
.Select(col => col.Collection))
.SingleOrDefault();

但是这会导致以下错误:



< blockquote>

Include路径表达式必须引用在类型上定义的导航属性
。使用虚线路径参考导航
属性和选择运算符用于集合导航
属性。参数名称:路径


任何想法这个错误是什么意思?



谢谢提前



编辑:



我的型号:

  public class Page 
{
public int PageId {get;组; }
public string标题{get;组; }
public ICollection< Series>系列{get;组;
}

public class Series
{
public int SeriesId {get;组; }
public int PageId {get;组; }
public string标题{get;组; }
public page Page {get;组; }
public ICollection< Comic>漫画{get;组; }
}

public class Comic
{
public int ComicId {get;组; }
public string标题{get;组; }
public int ReadingOrder {get;组; }
public string Subtitle {get;组; }
public int CollectionId {get;组; }
public Collection Collection {get;组; }

}

public class Collection
{
public int CollectionId {get;组; }
public string标题{get;组; }
public ICollection< Comic>漫画{get;组; }
}


解决方案

> ...包含路径表达式必须引用导航属性... 基本上抱怨 c.Comics.OrderBy 不是导航属性。 (这是一个合法的投诉,我想。)



实际上EF不支持在热切的加载语句中应用排序(以及过滤)(包括)。



所以,你能做什么?



选项1:



加载实体后,在内存中排序:

  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(model.Page.Series中的var series)
series.Comics = series.Comics.OrderBy (c => c.ReadingOrder).ToList();
}

丑陋,但因为您正在加载显然只有一个页面对象的id可能比以下选项更快(内存中的LINQ to Object)(如果系列漫画收藏不是特别长的。



选项2:



细分查询部分混合加载和显式加载:

  model.Page = db.Pages 
.Where(p => p.PageId == Id)
.Include(p => p.Series)//只包含系列集合
.SingleOrDefault();

if(model.Page!= null)
{
foreach(model.Page.Series中的var series)
db.Entry(series).Collection s => s.Comics).Query()
.Include(c => c.Collection)
.OrderBy(c => c.ReadingOrder)
.Load() ; //循环中每个系列的一个新的DB查询
}

选项3: p>

投影?



Here and 这里是关于复杂的的危险包含多个导航属性的链。它可以加载大量的重复数据。 Include 确保您只有一个DB往返,但可能要花费更多的传输数据。显式加载有多个往返程序,但总共可以减少数据。



(我知道,我给你这个包含...选择...选择...选择。 ..链,但我怎么知道你会带我认真的:)那么,根据你的嵌套集合的大小,它仍然是最好的选择。)


I've got the following query:

model.Page = db.Pages
    .Where(p => p.PageId == Id)
    .Include(p => p.Series
                   .Select(c => c.Comics
                                 .Select(col => col.Collection)))
    .SingleOrDefault();

This works great, although I now need to order the Comics by a property called 'ReadingOrder'.

I've tried:

model.Page = db.Pages
    .Where(p => p.PageId == Id)
    .Include(p => p.Series.Select(c => c.Comics.OrderBy(o => o.ReadingOrder)
                          .Select(col => col.Collection)))
    .SingleOrDefault();

But this results in the following error:

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

Any ideas what this error means?

Thanks in advance

EDIT:

My models:

public class Page
{
    public int PageId { get; set; }
    public string Title { get; set; }
    public ICollection<Series> Series { get; set; }
}

public class Series
{
    public int SeriesId { get; set; }
    public int PageId { get; set; }
    public string Title { get; set; }
    public Page Page { get; set; }
    public ICollection<Comic> Comics { get; set; }
}

public class Comic
{
    public int ComicId { get; set; }
    public string Title { get; set; }
    public int ReadingOrder { get; set; }
    public string Subtitle { get; set; }
    public int CollectionId { get; set; }
    public Collection Collection { get; set; }

}

public class Collection
{
    public int CollectionId { get; set; }
    public string Title { get; set; }
    public ICollection<Comic> Comics { get; set; }
}

解决方案

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

这篇关于实体框架,MVC 3,LINQ to Entities中的OrderBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆