首页 > 解决方案 > 从异步方法返回任务以外的对象

问题描述

如何从异步方法返回 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;
}

标签: c#asp.net-core

解决方案


异步函数应该返回一个任务。

await当你调用它时,你需要任务。

var result = await GetGroupedSearchStrings(shopID);

推荐阅读