title: How to await during Select in a lambda
body: <p>I am attempting to perform a Lambda Select using Entity Framework from a table into a new model but I need to be able to call an asynchronous method to populate a property on each instance in the return set:</p>
  
  <pre><code>await Task.WhenAll(_context.UserContacts.Where(uc =&gt; uc.UserId == user.Id).Select(async uc =&gt; new MailContact
              {
                  Email = uc.Contact.Email,
                  UserId = uc.Contact.UserId,
                  ContactId = uc.Contact.Id,
                  Name = uc.Contact.UserId != null ? await _graphService.GetUserByIdAsync(uc.Contact.UserId) : null;
              }
  </code></pre>
  
  <p>I understand that Linq has limited support for await/async and I've looked at several other examples on StackOverflow where the asynchronous part is moved into a seperate loop, eg:</p>
  
  <p><a href="https://stackoverflow.com/questions/16624648/how-to-await-a-method-in-a-linq-query">How to await a method in a Linq query</a></p>
  
  <pre><code>T[] data = await Task.WhenAll(contacts.Select(c =&gt; LoadDataAsync(c)));
  </code></pre>
  
  <p>However, this method wont let me update the object referred to as "c" unless I pass by reference which isn't allowed on async methods.</p>
  
  <p>Can someone explain the most efficient way to populate the name property correctly?</p>