我想使用EF 5检索一个对象加上它的筛选/有序集合属性。但是,我的当前代码抛出一个异常:
包含路径表达式必须引用在类型上定义的导航属性。引用导航属性使用虚线路径,集合导航属性使用Select
下面是我要检索的对象的类:
public class EntryCollection
{
[Key]
public int Id { get; set; }
public ICollection<Entry> Entries { get; set; }
...
}以下是Entry的定义
public class Entry
{
[Key]
public int Id { get; set; }
public DateTime Added { get; set; }
...
}我想检索只包含最新条目的EntryCollection,下面是我尝试过的代码:
using (var db = new MyContext())
{
return db.EntryCollections
.Include(ec => ec.Entries.OrderByDescending(e => e.Added).Take(5))
.SingleOrDefault(ec => ec.Foo == "bar');
}有什么想法吗?
发布于 2013-08-11 06:40:14
不能在包含中使用OrderBy。
那么以下几点呢?
using (var db = new MyContext())
{
return db.EntryCollections
.Where(ec => ec.Foo == "bar")
.Select(ec=> new Something{Entries = ec.Entries.OrderByDescending(e => e.Added).Take(5) }, /*some other properties*/)
.SingleOrDefault();
}或者在两个单独的查询中执行
https://stackoverflow.com/questions/18169424
复制相似问题