[英]How to await LINQ query with multiple from clauses?
I have the following LINQ query that uses multiple from clauses: 我有以下使用多个from子句的LINQ查询:
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? 我该如何调整它,以便等待每个from子句? 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. 我知道有很多关于如何在LINQ中使用await的示例,但是我找不到如何处理多个from子句的示例。 I'm using SQLite-net with the async extensions. 我正在使用带有异步扩展的SQLite-net。
edit 1: I tried the following, as per Evk's suggestion: 编辑1:按照Evk的建议,我尝试了以下操作:
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'. 找不到源类型“ AsyncTableQuery”的查询模式的实现。 'SelectMany' not found. 找不到“ SelectMany”。
Try using Task.WhenAll() . 尝试使用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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.