asynchronous - How to await a ParallelQuery with LINQ? -


i have async method, should database entries. filters name, candiate parallel execution.

however, can not find simple way support both parallel execution , asynchronous tasks.

here's have:

private async task<list<item>> getmatchingitems(string name) {     using (var entities = new entities()) {         var items =  item in entities.item.asparallel()                item.name.contains(name)                select item;         return await items.tolistasync(); //complains "parallelquery<item> not contain definition tolistasync..."     } } 

when remove asparallel() compile. not supposed use both features @ same time? or understand wrong?

ihmo, both make sense:

  • asparallel() indicate database query may split several sub-queries running @ same time, because individual matching of item not dependent on other item. update: bad idea in example, see comments , answer!
  • tolistasync() support asynchronous execution of method, allow other match methods (on other data) start executing immediately.

how use both parallel exectuion (with linq) , asynchronous tasks @ same time?

you can't, , shouldn't. plinq isn't database access. database knows best on how parallelize query , fine on it's own using normal linq. plinq accessing objects , such doing computationally expensive calculations within linq query can parallelize on client side (vs parallelizing on server/database server side).

a better answer might be: plinq distributing query compute intensive across multiple threads. async returning thread others can use because going waiting on external resource (database, disk i/o, network i/o, etc).

async plinq doesn't have strong use case want return thread while wait , have lot of calculations do... if busy calculating, need thread (or multiple threads). on different ends of optimization. if need this, there better mechanisms tasks, etc.


Comments