简体   繁体   中英

Return an object other than a task from an async method

How do I return an object other than Task from an async method? I do some async work in the method, but I need to return something other than Task or return the object inside a Task. I don't know.

Code:

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;
}

Async functions should be returning a Task.

You need to await the Task when you call it.

var result = await GetGroupedSearchStrings(shopID);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM