我正在处理的项目在 OData 层之上具有实体框架。Odata 层将其服务器端分页设置为75
. 我对这个主题的阅读让我相信这个分页值是全面使用的,而不是每张桌子的基础。我目前要从中提取所有数据的表当然超过 75 行。使用实体框架,我的代码就是这样:
public IQueryable<ProductColor> GetProductColors()
{
return db.ProductColors;
}
db
实体上下文在哪里。这将返回前 75 条记录。我读了一些东西,我可以在其中附加一个参数inlinecount
集来allpages
给我以下代码:
public IQueryable<ProductColor> GetProductColors()
{
return db.ProductColors.AddQueryOption("inlinecount","allpages");
}
但是,这也返回 75 行!
无论 OData 服务器端分页内容如何,任何人都可以阐明如何真正获取所有记录吗?
重要:我无法删除分页或将其关闭!在其他需要关注性能的场景中,它非常有价值。
更新: 通过更多搜索,我找到了描述如何执行此任务的MSDN 。
我希望能够将它变成一个完整的通用方法,但是,这与我可以在不使用反射的情况下获得的通用方法一样接近:
public IQueryable<T> TakeAll<T>(QueryOperationResponse<T> qor)
{
var collection = new List<T>();
DataServiceQueryContinuation<T> next = null;
QueryOperationResponse<T> response = qor;
do
{
if (next != null)
{
response = db.Execute<T>(next) as QueryOperationResponse<T>;
}
foreach (var elem in response)
{
collection.Add(elem);
}
} while ((next = response.GetContinuation()) != null);
return collection.AsQueryable();
}
像这样称呼它:
public IQueryable<ProductColor> GetProductColors()
{
QueryOperationResponse<ProductColor> response = db.ProductColors.Execute() as QueryOperationResponse<ProductColor>;
var productColors = this.TakeAll<ProductColor>(response);
return productColors.AsQueryable();
}