gkelly
gkelly

Reputation: 288

Async issue regarding types

I have recently switched from Entity Framework To Dapper. So far I am really pleased with the results; however, I am having an async issue though that I'm having difficulty resolving.

An example is the following:

var list1 = await _db.getList(...);   // async Task<Element> getList(...)
var list2 = v_list1.select( async item => {

  var flag = await _db.getFlagValue(...); // async Task<bool> getFlagValue(...)
  item.flag = flag;
  return item;

});

because of the "async" before 'item' typeof list2 is IEnumerable<Task<Element>> instead of IEnumerable<Element>.

If I change getFlagValue to be sync instead of async and change the code then it works fine.

I tried adding an additional

.Select( async r => await r );

to the end but it didnt work. Though the typeof for list2 was correct, the it hung at runtime.

Hopefully I have provided enough code to show the issue.

Upvotes: 0

Views: 48

Answers (1)

SLaks
SLaks

Reputation: 887453

You need await Task.WhenAll(list2).

Task.WhenAll() takes a collection of tasks and returns a task of a collection of results.

Upvotes: 2

Related Questions