gpt4 book ai didi

c# - 无法将任务> 转换为任务>

转载 作者:行者123 更新时间:2023-12-04 12:42:39 25 4
gpt4 key购买 nike

我正在围绕 EF Core 编写一个小型包装器方法 DbSet .我有以下方法:

public Task<IList<TEntity>> GetAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> getFunction)
{
if (getFunction == null)
{
Task.FromResult(new List<TEntity>());
}
return getFunction(_dbSet).AsNoTracking().ToListAsync();
}

如您所见,该类是通用的,_dbSet 是具体 DbSet 的一个实例从上下文。然而,这对问题来说并不重要。
对于代码,我收到以下错误:

[CS0029] Cannot implicitly convert type 'System.Threading.Tasks.Task>' to 'System.Threading.Tasks.Task>'

如果我将返回值更改为 Task<List<TEntity>>没有错误。
有谁知道为什么它不能转换它?谢谢!

最佳答案

我认为最简单的方法是等待任务。因此它将以最少的更改工作:

public async Task<IList<TEntity>> GetAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> 
getFunction)
{
if (getFunction == null)
{
return new List<TEntity>();
}
return await getFunction(_dbSet).AsNoTracking().ToListAsync();
}

关于c# - 无法将任务<List<TEntity>> 转换为任务<IList<TEntity>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56477537/

25 4 0