title: Async method in query syntax
body: <p>I learn with asp.net identity, async/await, and I have this problem:</p>
  
  <p>I have some function for <code>IEnumerable&lt;T&gt; list</code>. This list I fill using query syntax and looks like this:</p>
  
  <pre><code>private IEnumerable&lt;SomeModel&gt; GetPersons(int categoryId) {
  IEnumerable&lt;SomeModel&gt; persons = from g in db.Categories
                                 join c in db.Persons on g.PersonTypeId equals c.PersonTypeId
                                 where g.CategoryId == categoryId
                                 select new SomeModel
                                 {
                                     PersonName = c.FirstName + " " + c.LastName,
                                     //....etc.
                                     //And here I need call asynchronous function something like this:
                                     IsAdmin = GetPermission(c.Email)
                                 }
      if (persons.Any()) {
          return persons;
      }
  
      return Enumerable.Empty&lt;SomeModel&gt;();
  }
  </code></pre>
  
  <p>In SomeModel is <code>IsAdmin</code> as bool (when I tried <code>Task&lt;bool&gt;</code> in GetPermission I use <code>Task&lt;bool&gt;</code> in SomeModel).
  In <code>GetPermission()</code> is this:</p>
  
  <pre><code>private bool GetPermission(string email) {
      var user = SomeMembershipService.GetUser(email); //SomeMembershipService is Interface with Tasks and so on.
      var roles = SomeMembershipService.GetRoles(user.Id); //user.Id is as string
      bool result = false;
      if (roles != null) {
          var adm = roles.Result.FirstOrDefault(x =&gt; x.Name.Contains("Admin"));
          result = adm != null;
      }
      return result;
  }
  </code></pre>
  
  <p>I tried this write with async/await and as Task, but both my tries was false. So I thought that I have to call <code>GetPermission()</code> outside of <code>IEnumerable&lt;SomeModel&gt; persons</code> so I add this block of code into condition after. So code looks like this:</p>
  
  <pre><code>private IEnumerable&lt;SomeModel&gt; GetPersons(int categoryId) {
      IEnumerable&lt;SomeModel&gt; persons = from g in db.Categories
                                     join c in db.Persons on g.PersonTypeId equals c.PersonTypeId
                                     where g.CategoryId == categoryId
                                     select new SomeModel
                                     {
                                         PersonName = c.FirstName + " " + c.LastName
                                         //....etc.
                                     }
      if (persons.Any()) 
      {
          //new code
          foreach (var p in persons) 
          {
              p.IsAdmin = GetPermission(p.Email);
          }
          //end of new code
          return persons;
      }
  
      return Enumerable.Empty&lt;SomeModel&gt;();
  }
  </code></pre>
  
  <p>But this is wrong too. Maybe I understand bad to asp.net identity and async/await and their use...
  Can you help me - what I have to do? Because now, <code>GetPermission</code> is called too late, so app crash. When I call <code>GetPermission</code> in persons query, function is called too late (after filled persons list) too. I have no idea how to continue. </p>
  
  <p><code>GetUser()</code> is as <code>public IUser GetUser(string username)</code> and <code>GetRoles()</code> is as <code>public async Task&lt;IEnumerable&lt;IRole&gt;&gt; GetRoles(string userId)</code>. I'am sure, that these two methods work fine, I use them in other codes and no problem with them. So it has to be somewhere in code above I think.</p>
  
  <p>I am sorry, if it is stupid question, but I read about this lot here and on msdn, but cannot find result. Thanks to all.</p>
  
  <hr>
  
  <p><strong>Why I want to use async function as above</strong></p>
  
  <p>I want the function there because when I make whole function as async Task, another function which call this doesn't work properly. </p>
  
  <p>I have this function - it's databinding for kendogrid ():</p>
  
  <pre><code>[HttpPost]
  public ActionResult _PersonsBinding([DataSourceRequest]DataSourceRequest request, int id)
  {
      DataSourceResult result = GetPersons(id).ToDataSourceResult(request);
      return Json(result);
  }
  </code></pre>
  
  <p>When I make function <code>IEnumerable&lt;SomeModel&gt; GetPersons</code> as <code>async Task&lt;IEnumerable&lt;SomeModel&gt;&gt; GetPersons</code> the binding function doesn't know ToDataSourceResult(), when I make this function as async too. If can be problem here, how can I fix it? 
  Please be patient with me, I am a newbie...</p>