[英]What can I use instead of Where clause in Asyn method?
我在c#和Entity Framework中有一个项目。 我需要将存储库的某些方法转换为异步。 我在转换此方法时遇到问题,不确定是否可以代替Where子句使用。
public async Task<IEnumerable<Product>> GetProductByYearIdAsync(int yearId)
{
return await ApplicationsContext.Product.Where(product=> product.YearId == yearId);
}
当我使用它时,它给了我无法等待iQueryable的错误。 当我使用FirstOrDefaultAsync时,我非常知道如何转换为IEnumerable。
您最后缺少.ToListAsync()
。 那是实际的异步操作,而不是.Where(
请注意,必须确保程序集引用EntityFramework
dll,并且在using语句中包含名称空间System.Data.Entity
,因为这是扩展方法 。
Where
实际上并不执行查询,因此调用Where
并没有“异步”。 您需要调用实际上异步执行查询的ToListAsync
。
public async Task<IEnumerable<Product>> GetProductByYearIdAsync(int yearId)
{
return await ApplicationsContext.Product
.Where(product=> product.YearId == yearId).ToListAsync();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.