当用户要求大型数据集时,发送多个API请求
我已经使用ODATA创建了一个GET请求,但是当我尝试检索所有数据时,API很慢。我想做的是将请求分为几个多个请求。当返回每个请求时,我想将其附加到客户端端的内容。我已经放入了一些代码的片段,并且省略了其中的一部分,例如检查零值/解析等。是否可以从一个控制器中进行操作,还是我需要做其他事情?
详细的解释将不胜感激!
[HttpGet]
[Microsoft.AspNetCore.OData.Query.EnableQuery(PageSize = pageSize)]
public IActionResult GetVendors(ODataQueryOptions<VendorsModel> options)
{
int accountID = int.Parse(options.Filter.RawValue);
int pageSize = 5000;
IQueryable<model> model = Enumerable.Empty<model>().AsQueryable();
var top = int.Parse(options.Top.RawValue);
int remaining = top;
if (top > pageSize)
{
do
{
model = Enumerable.Empty<model>().AsQueryable(); //reset model so I do not have any duplicates when I append the data.
model = context.GetModel.Where(x => x.AccountID == accountID).Skip(top - remaining).Take(pageSize);
Ok(model); //This is where I would like to update the front-end from.
remaining -= pageSize;
} while (remaining > pageSize);
model = context.GetModel.Where(x => x.AccountID == accountID).Skip(top - remaining).Take(remaining);
return Ok(model); // return final part of the data.
}
}
I've created a GET request with Odata but the API is slow when I try to retrieve all of the data. What I wanted to do was split the request to a few multiple requests. As each request is returned then I wanted to append it to what's already on the client end. I've put in a snippet of some of my code and I've omitted parts of it eg checking for null values/parsing etc. Is it possible to do it from one controller or would I need to do something else?
A detailed explanation would be appreciated!
[HttpGet]
[Microsoft.AspNetCore.OData.Query.EnableQuery(PageSize = pageSize)]
public IActionResult GetVendors(ODataQueryOptions<VendorsModel> options)
{
int accountID = int.Parse(options.Filter.RawValue);
int pageSize = 5000;
IQueryable<model> model = Enumerable.Empty<model>().AsQueryable();
var top = int.Parse(options.Top.RawValue);
int remaining = top;
if (top > pageSize)
{
do
{
model = Enumerable.Empty<model>().AsQueryable(); //reset model so I do not have any duplicates when I append the data.
model = context.GetModel.Where(x => x.AccountID == accountID).Skip(top - remaining).Take(pageSize);
Ok(model); //This is where I would like to update the front-end from.
remaining -= pageSize;
} while (remaining > pageSize);
model = context.GetModel.Where(x => x.AccountID == accountID).Skip(top - remaining).Take(remaining);
return Ok(model); // return final part of the data.
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论