我正在尝试使用此处提供的 PagedList.Mvc 库:https://github.com/TroyGoode/PagedList。
以下是该库的使用示例:
以下是该库的使用示例:
var products = MyProductDataSource.FindAllProducts(); //returns IQueryable<Product> representing an unknown number of products. a thousand maybe?
var pageNumber = page ?? 1; // if no page was specified in the querystring, default to the first page (1)
var onePageOfProducts = products.ToPagedList(pageNumber, 25); // will only contain 25 products max because of the pageSize
MyProductDataSource.FindAllProducts()的典型实现通常如下:
public IQuerable<T> MyProductDataSource.FindAllProducts()
{
using ( var ctx = new MyCtx() )
{
return ctx.MyList().Where( .... );
}
}
当然会出现InvalidOperationException()和DBContext已释放的信息。
想要找到最佳实践,以便在此处使用IQueryable时不会出现问题?