在Asyn方法中我可以使用什么代替Where子句?

时间:2016-02-02 22:02:11

标签: c# entity-framework asynchronous

我在c#和Entity Framework中有一个项目。我需要将Repository的一些方法转换为Async。我有转换此方法的问题,不知道我可以使用什么而不是Where子句。

std::atomic<int>

当我使用它时,它给了我无法等待iQueryable的错误。当我使用FirstOrDefaultAsync时,我很多人都知道如何转换为IEnumerable。

2 个答案:

答案 0 :(得分:3)

你最后遗漏了.ToListAsync()。这是实际的异步操作,而不是.Where(

请注意,您必须确保程序集正在重新声明EntityFramework dll,并且您在using语句中包含名称空间System.Data.Entity,因为这是a extension method

答案 1 :(得分:1)

Where实际上并不执行查询,因此没有任何关于调用Where的“异步”。您需要调用实际以异步方式执行查询的ToListAsync

public async Task<IEnumerable<Product>> GetProductByYearIdAsync(int yearId)
{
    return await ApplicationsContext.Product
        .Where(product=> product.YearId == yearId).ToListAsync();
}