'Send multiple API requests when user asks for large dataset
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.
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
