如何使用 IQueryable 获得异步响应?

问题描述 投票:0回答:1

我收到警告消息

此异步方法缺少“await”运算符,并将同步运行。考虑使用“await”运算符等待非阻塞 API 调用,或使用“await Task.Run(...)”在后台线程上执行 CPU 密集型工作。

产生此警告的代码是

app.MapGet("/questionsByCategory", async (Context dbContext, int categoryId, bool inTest) =>
        {
            IQueryable<Questions> result = dbContext.Questions
                .AsNoTracking()
                .Where(x => x.CategoryId == categoryId)
                .Include(x => x.Answers)
                .AsQueryable();

            if (inTest)
                result.Where(x => x.InTest == true);

            return Results.Ok(result);
        })
        .WithName("questionsByCategory")
        .WithOpenApi();
c# entity-framework linq iqueryable
1个回答
0
投票

您可以在

.ToListAsync()
之后致电
.AsQueryable()

更换

IQueryable<Questions> result = dbContext.Questions
    .AsNoTracking()
    .Where(x => x.CategoryId == categoryId)
    .Include(x => x.Answers)
    .AsQueryable();

List<Questions> result = await dbContext.Questions
    .AsNoTracking()
    .Where(x => x.CategoryId == categoryId)
    .Include(x => x.Answers)
    .AsQueryable()
    .ToListAsync();
© www.soinside.com 2019 - 2024. All rights reserved.