Vit*_*nko 3 c# asynchronous covariance contravariance async-await
我正在围绕 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();
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,该类是通用的,并且 _dbSet 是DbSet上下文中具体的实例。然而,对于这个问题来说,这并不重要。
对于代码我得到以下错误:
[CS0029] 无法将类型“System.Threading.Tasks.Task>”隐式转换为“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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2012 次 |
| 最近记录: |