title: How do I use async Task<IActionResult> ? Or How to run in async way in my Asp.Net Core Web Api
body: <p>I'am trying to run my Controller Action in async way.
  How do I use async Task ? Or How to run in async way</p>
  
  <pre><code>// Db context
  public class DeptContext : DbContext
  {
      public LagerContext(DbContextOptions&lt;LagerContext&gt; options)
          : base(options)
      {
  
          Database.Migrate();
      }
  
      public DbSet&lt;Department&gt; Departments { get; set; }
      public DbSet&lt;Product&gt; Products { get; set; }
  
  
  }
  </code></pre>
  
  <p>// This is my Interface IDepRepository</p>
  
  <pre><code>Task&lt;Department&gt; GetDepartmentWithOrWithoutProducts(int deptId, bool includeProducts);
  </code></pre>
  
  <p>// And my Repository class DepRepository</p>
  
  <pre><code>public class DepRepository : IDepRepository
  {
     private DeptContext db;
      public DepRepository(DeptContext context)
      {
          db = context;
      }
  
      // I'am geting Department name with products or Without products
  
      public async Task&lt;Department&gt; GetDepartmentWithOrWithoutProducts(int deptId, bool includeProducts)
       {
         if(includeProductss)
          {
            return await db.Departments.Include(c =&gt; c.Products).Where(s =&gt; s.deptId == deptId).SingleAsync();
          }
                  return await db.Departments.Where(s =&gt; s.deptId == deptId).SingleAsync();
      }
  }
  </code></pre>
  
  <p>So How should I do now in my Controller to do it as async way: I tried as following but I don't know if it's right to do like this following: 
  I'm not getting any error but I don't if it's right way ...</p>
  
  <pre><code>using System.Threading.Tasks;
  using System.Net;
  using Microsoft.Data.Entity;
  using Microsoft.EntityFrameworkCore;
  
  [Route("api/departments")]
  public class DepartmentsController : Controller
  {
      private IDeptRepository _deptInfoRepository;
  
      public DepartmentsController(IDeptRepository deptInfoRepository)
      {
          _deptInfoRepository = deptInfoRepository;
      }
  
      [HttpGet("{id}")]
      public async Task&lt;IActionResult&gt; GetDepatment(int id, bool includeProducts = false)
      {
          var dept = _deptInfoRepository.GetDepartmentWithOrWithoutProducts(id, includeComputers);
          if(dept == null)
          {
              return BadRequest();
          }
  
          if(includeProducts)
          {
              var depResult =  new DepartmentDto() { deptId = dept.deptId, deptName = dept.deptName };
              foreach(var department in dept.Products)
              {
                  depResult.Products.Add(new ProductDto() { productId = department.productId, deptId = department.deptId, ProductName =                     department.ProductName });
              } 
  
              return Ok(depResult);
          }
  
          var departmentWithoutProductResult = new DepartmentsWithoutProductsDto() { DeptId = dept.deptId, DeptName = dept.DeptName};
          return Ok(departmentWithoutProductResult);
  
  
      }
  </code></pre>
  
  <p>How do I do to get my controller in async way.. I don't know where to put those await and ToListAsync(). Thank you in advance!</p>