0

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.

lepton
  • 715
  • 1
  • 6
  • 22
  • 3
    Just do `await (......).CountAsync()`. Number of from clauses doesn't matter for that. – Evk Mar 29 '18 at 10:46
  • @Marc - I want to await all three db.Table calls. – lepton Mar 29 '18 at 10:48
  • 4
    There should not be 3 db calls. It should be converted to one sql query. – Evk Mar 29 '18 at 10:50
  • @Evk - I tried your suggestion (see my edit), but get a compiler error. Can you spot what I'm doing wrong? – lepton Mar 29 '18 at 11:10
  • convert to sql query and use join – komluk Mar 29 '18 at 11:12
  • Well I'm not aware of ORM\provider you are using. If that were Entity Framework for example - `CountAsync` should work. Here you are using some `CountAsAsync` which I didn't met before. Maybe someone with more knowledge of this provider can help. – Evk Mar 29 '18 at 11:19
  • If you use `await`, you must mark your method with `async`, [ref](https://msdn.microsoft.com/en-us/library/hh191443(v=vs.120).aspx) – Martin Verjans Mar 29 '18 at 12:57
  • 1
    l see no reason why the first query should compile and the second not. – Paulo Morgado Mar 31 '18 at 20:03

1 Answers1

0

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();