c# - 从异步方法返回任务以外的对象
问题描述
如何从异步方法返回 Task 以外的对象?我在方法中做了一些异步工作,但我需要返回任务以外的东西或返回任务中的对象。我不知道。
代码:
public async Task<ICollection<KeyValuePair<int, IServiceProvider>>> GetGroupedSearchStrings(int shopId)
{
ICollection<KeyValuePair<int, ISearchString>> result = new Collection<KeyValuePair<int, ISearchString>>();
IEnumerable<ISearchString> ss = await ShopsExpressions.SearchString(this.Worker.GetRepo<SearchString>().DbSet).Where(s => s.Active && s.ShopId.Equals(shopId)).OrderBy(s => s.DateCreated).ToListAsync();
result.Add(new KeyValuePair<int, ISearchString>(1, DEMO));
return result;
}
解决方案
异步函数应该返回一个任务。
await
当你调用它时,你需要任务。
var result = await GetGroupedSearchStrings(shopID);
推荐阅读
- c++ - 来自 csv 的 C++ 信息在数组 [][] 中变得杂乱无章
- c++ - 即使 a 被证明是错误的,“while(bool a && bool b)”也会检查 a 和 b 吗?
- vue.js - 本地主机上的灯塔性能分数为零,但部署到外部网络主机时我得到 30
- c# - 动态 LINQ 过滤
- json - 尝试使用 React 使用 Google Books API 访问 JSON 中的元素
- xml - 读取另一个 xml 文件并根据 xslt 中的元素序列顺序进行排序
- jquery - 如何使用 jQuery for SharePoint 2013 网站在 datetimepicker 中获取带有日期的时间戳?
- python - 为什么 Python 代码中有 8128 8128 (“不是”与“!=”有什么不同)
- javascript - 服务器文件在本地工作正常,但一旦部署(在 Heroku 上),MongoDB 返回“false”而不是 JSON
- java - 如何一次使用 openGL ES 渲染多纹理?