How can I use the async/await keywords correctly in a lambda expression ? here is the code :
public async Task<IHttpActionResult> GetUsers() {
var query = await _db.Users.ToListAsync();
var users = query.Select(async u => new
{
FirstName = u.FirstName,
LastName = u.LastName,
IsGeek = await _userManager.IsInRoleAsync(u.Id, "Geek")
});
return Ok(users);
}
As you can see this code is running inside a webapi controller, it compiles without any error, the problem is it needs an extra await
somewhere because this action never retuns.
Notice that _db
and _usermanager
are the DbContext
and the UserManager
for the application.
Thanks.
Update :
This equivalent code never fails (but it's not ellegant :( ):
var query = await _db.Users.ToListAsync();
var users = new List<object>();
foreach (var u in query)
{
bool IsGeek = await _userManager.IsInRoleAsync(u.Id, "IsGeek");
users.Add( new {
FirstName = u.FirstName,
LastName = u.LastName,
IsGeek= IsGeek
});
}
return Ok(users);
Think about your types.
query
is a list of users.When you
Select
with anasync
lambda, the result is a sequence of tasks. So, to (asynchronously) wait for all those tasks to complete, useTask.WhenAll
: