简体   繁体   中英

How to await LINQ query with multiple from clauses?

I have the following LINQ query that uses multiple from clauses:

var count = (
    from uTask in db.Table<UTask>().Where(u => u.ParentId == componentId && u.RouteId == routeId)
    from workItem in db.Table<WorkItem>().Where(w => w.ParentId == uTask.Id)
    from visualInspectionQuestion in db.Table<VisualInspectionQuestion>().Where(v => v.ParentId == workItem.Id && v.Answer != null)
    select new { }).Count();

How can I adjust this so that each of the from clauses are awaited? I'm aware that there a numerous examples of how to use await with LINQ, but I cant find an example of how to handle multiple from clauses. I'm using SQLite-net with the async extensions.

edit 1: I tried the following, as per Evk's suggestion:

count = await (
    from uTask in db.Table<UTask>().Where(u => u.ParentId == componentId && u.RouteId == routeId)
    from workItem in db.Table<WorkItem>().Where(w => w.ParentId == uTask.Id)
    from visualInspectionQuestion in db.Table<VisualInspectionQuestion>().Where(v => v.ParentId == workItem.Id && v.Answer != null)
    select new { }).CountAsync();

However, this produces the following compiler error:

Could not find an implementation of the query pattern for source type 'AsyncTableQuery'. 'SelectMany' not found.

Try using Task.WhenAll() . This will wait for all your tasks to finish before counting:

var tasks = foos.Select(DoSomethingAsync).ToList();
var count = await Task.WhenAll(tasks).CountAsync();

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