title: how to use async/await inside a lambda
body: <p>How can I use the async/await keywords correctly in a lambda expression ? here is the code :</p>
  
  <pre><code>public async Task&lt;IHttpActionResult&gt; GetUsers() {
  
          var query = await _db.Users.ToListAsync();
  
          var users = query.Select(async u =&gt;  new
          {
              FirstName = u.FirstName,
              LastName = u.LastName,
              IsGeek = await _userManager.IsInRoleAsync(u.Id, "Geek")
          });
  
          return Ok(users);
  }
  </code></pre>
  
  <p>As you can see this code is running inside a webapi controller, it compiles without any error, the problem is it needs an <strong>extra</strong> <code>await</code> somewhere because this action never retuns. </p>
  
  <p>Notice that <code>_db</code> and <code>_usermanager</code> are the <code>DbContext</code> and the <code>UserManager</code>for the application.</p>
  
  <p>Thanks.</p>
  
  <p><strong>Update</strong> :</p>
  
  <p>This equivalent code never fails (but it's not ellegant :( ): </p>
  
  <pre><code>var query = await _db.Users.ToListAsync();
  var users = new List&lt;object&gt;();
  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);
  </code></pre>