title: await with lambda expressions only
body: <p>I'm having trouble finding the correct syntax inorder to use await with lambda expressions (anonymous lambda methods). All examples seem to use actual methods declared with the async keyword. Let me describe what I want:  </p>
  
  <pre><code>...code in UI thread...
  Customer cust = null;
  using (DataContext context = new DataContext()) // I want to run this async
  {
     cust = context.Customers.Single(c =&gt; c.Id == 1);
  }
  ...continue code in UI thread...
  </code></pre>
  
  <p>Inorder to not block the UI thread during the database query, I would write:</p>
  
  <pre><code>...code in UI thread...
  Customer cust = null;
  Task.Run(() =&gt; 
  {
     using (DataContext context = new DataContext())
     {
        cust = context.Customers.Single(c =&gt; c.Id == 1);
     }
  });
  ...continue code in UI thread...
  </code></pre>
  
  <p>This would not work of course, because the UI thread would continue after the task had been started. I cannot <code>Wait()</code> on the <code>Task</code>, because that would block the UI thread. Simply adding <code>await</code> in front of <code>Task.Run()</code> does not compile. The best thing that I could come up with was:</p>
  
  <pre><code>...code in UI thread...
  Customer cust = null;
  Parallel.Invoke(async () =&gt;
  {
     await Task.Run(() =&gt; 
     {
        using (DataContext context = new DataContext())
        {
           cust = context.Customers.Single(c =&gt; c.Id == 1);
        }
     });
  });
  ...continue code in UI thread...
  </code></pre>
  
  <p>Now I haven't tested this yet, so I don't know if this would even actually work or still block the UI thread. But I don't like the <code>Parallel.Invoke</code> call anyway, I'm sure there is a cleaner/nicer way of invoking the anonymous method, but at the moment I can't come up with anything. Also the main issue I have, is that my gut is telling me that inorder to achieve asynchronous continuation (invoke database query async, don't block UI thread during it, then continue with what ever code follows the async call on the UI thread), the very first line must start with <strong>await ...</strong><br>
  Eg:</p>
  
  <pre><code>...code in UI thread...
  Customer cust = null;
  await ...(whatever follows)
  ...continue code in UI thread...
  </code></pre>
  
  <p>Or maybe even:</p>
  
  <pre><code>...code in UI thread...
  Customer cust = await ...(some anonymous lambda method returning Customer)
  ...continue code in UI thread...
  </code></pre>
  
  <p>So my question is. How to correctly write this piece of code without using any named methods, only lambda expressions (anonymous lambda methods).</p>