c# - 无法转换任务> 到任务>
问题描述
我正在围绕 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] 无法将类型“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();
}
推荐阅读
- json - 从 API 获取对象列表反应原生
- python-3.x - python3:来自正则表达式匹配失败的详细错误信息?
- php - Amadeus Flight search api在curl php中传递access_token时出错
- java - 通过 IMAP java 邮件更改电子邮件主题的解决方法
- ios - 如何从在线和离线获取数据并在tableView swift中设置
- c++ - GitHub Actions CI 中的 Ccache 命中率 0.00 %
- electron - Spectron 从动态文本框中获取文本数据
- c# - C#将文件路径传递给其他方法
- java - Java-如何在我的文本变量中保存 JTable 单元格
- javascript - 如何删除公会 discord.js 中的所有频道