title: Entity Framework async record count with records
body: <p>I'm trying to fetch records from database but in the same call to DB get record count. My methods are async so I'm not sure if this is the right way to do it:</p>
  
  <pre><code>    public async Task&lt;IEnumerable&lt;ProductItemViewModel&gt;&gt; GetAsync(int pageIndex, int pageSize)
      {
          var products = this.Repository.GetAll&lt;Product&gt;().OrderBy(p =&gt; p.Price).AsQueryable();
  
          var count = await products.CountAsync();
          if (count == 0)
              return new List&lt;ProductItemViewModel&gt;();
  
          products = products.ToPaginatedList(pageIndex, pageSize);
          var result = await products.ToListAsync();
  
          var viewModels = new List&lt;ProductItemViewModel&gt;();
          Mapper.Map(result, viewModels);
  
          return viewModels.AsEnumerable();
      }
  </code></pre>
  
  <p>I think its not so good to await CountAsync() but I'm not sure how to implement it.</p>
  
  <p>Regards.</p>
  
  <p>EDIT:</p>
  
  <p>I apologize for not returning count anywhere, as it looks now that I only count because of that == 0 check, but I will eventually return count with records because I need it for my angular paging directive. Haven't figured it out how I will return (new class vs tuple) so I missed it out and focused on entity framework Count() and entity fetch in single DB call.</p>