简体   繁体   中英

The 'await' operator can only be used within an async lambda expression error

I have this function:

    public async Task<IEnumerable<RegionBreif>> GetAsync()
    {

    return await Table.Where(x => x.SiteId == _siteId).Select(r => new RegionBreif
    {
        IsServiceable = await RegionBreif.checkIfServicable(Context, r.SiteId, r.Id)
    }).ToArrayAsync();
}

And this function:

public static async Task<bool?> checkIfServicable(DbContext context, int? _siteId, int _regionId)
{
    var t = (from ir in context.Set<InspectionReview>()
             join so in context.Set<SiteObject>() on ir.ObjectId equals so.Id
             where so.SiteRegionId == _regionId && so.SiteId == _siteId
             select (bool?)
             ir.IsNormal);

    return t.Count() == 0 ? (bool?)null : t.Any(x => x.Value == false) ? false : true;
}

On this row:

await RegionBreif.checkIfServicable(Context, r.SiteId, r.Id)

I get this error:

Error 13 The 'await' operator can only be used within an async lambda expression. Consider marking this lambda expression with the 'async' modifier.

The problem is in the lambda expression you pass to the Select . If you want to make use of the await inside the type create, RegionBreif , you have to pass an async lambda expression as below:

Select(async r => new RegionBreif
{
    IsServiceable = await RegionBreif.checkIfServicable(Context, r.SiteId, r.Id)
})

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