Linq 使用IQueryables的缓存策略

Linq 使用IQueryables的缓存策略,linq,caching,repository-pattern,iqueryable,Linq,Caching,Repository Pattern,Iqueryable,我一直在思考如何最好地缓存IQueryables。我正在使用存储库模式将数据提取到一个类中,例如: public class Item { public int Id { get; set; } public string Name { get; set; } public IQueryable<Category> { get; set; } // For simplicity. It is actually a LazyList<Category>

我一直在思考如何最好地缓存IQueryables。我正在使用存储库模式将数据提取到一个类中,例如:

public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IQueryable<Category> { get; set; } // For simplicity. It is actually a LazyList<Category> containing an IQueryable
}
公共类项目
{
公共int Id{get;set;}
公共字符串名称{get;set;}
为简单起见,公共IQueryable{get;set;}//实际上是一个包含IQueryable的懒散列表
}
现在,我最好如何缓存这个对象?我希望在queryable上保留延迟执行,因为实际上除了类别之外,还有很多东西可以链接到项,我最好不要将它们提取到对象上存储,因为对象可能变得太大,或者它们可能需要单独缓存


我在想a能以某种方式使这成为可能,我只是不太明白怎么做。有人找到了解决这个问题的好办法吗?这似乎是一种非常常用的模式。

使用已编译的查询,可以缓存生成的委托,类型为
Func
。每当需要新的可查询项时,只需创建一个新的数据上下文,将其传递到委托中,并像正常情况一样对生成的
IQueryable
进行查询。

使用编译查询,您将缓存生成的委托,类型为
Func
。无论何时,只要创建一个新的查询表,就可以创建一个新的数据上下文传递到委托中,并像正常情况一样对结果
IQueryable
进行查询