当用户要求大型数据集时,发送多个API请求

发布于 2025-01-21 19:49:27 字数 1244 浏览 1 评论 0原文

我已经使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文