title: How to use Dapper Async method correctly & get data from Task<IEnumerable<T>>
body: <p>I am trying to learn using QueryAsync method to retrieve multiple records and returning a list of objects.  When the async call returns Task&lt;IEnumerable&gt;, I could not figure out how to actually extract data from IEnumerable.</p>
  <p><strong>TEST ENVIRONMENT</strong></p>
  <p>App:  .NET 6 console app with class library:  DapperDbOps
  IDE: Visual Studio 2022
  Test framework: XUnit</p>
  <p><strong>TEST CODE</strong></p>
  <p>In class library, I created class:  DapperDbOps.DapperHelper.  It contains a method: QueryMultipleAsync (see code below). I tried to return type as &quot;Task &lt;List&gt;&quot; and got complier error.  It looks like the only way to make the code work is using &quot;async Task&lt;IEnumerable&gt;&quot;</p>
  <pre><code>public async Task &lt;IEnumerable&lt;T&gt;&gt; QueryMultipleAsync&lt;T&gt;(string sqlCmd)
  {
      using IDbConnection db = new SqlConnection(_dbConnStr);
  
      var dtList = await db.QueryAsync&lt;T&gt;(sqlCmd).ConfigureAwait(false);
  
      return dtList;
  }
  </code></pre>
  <p>Under DapperDbOps, I created a data object class:  EmployeeDO.cs</p>
  <pre><code>namespace DapperDBOps.Models
  {
      public class EmployeeDO
      {
          public int EmployeeId { get; set; }
          public string Name { get; set; }
          public string Gender { get; set; }
          public int Salary { get; set; }
          public string Dept { get; set; }
      }
  }
  </code></pre>
  <p>In XUnit, setup the test project: DapperDbOpsUnitTests, and the test case DapperDbHelperTests.<br />
  To test QueryMultipleAsync, I setup the test method: QueryMultipleAsyncTest (see following code).</p>
  <pre><code>[Fact]
  public void QueryMultipleAsyncTest()
  {
      //Arrange
      string sqlCmd = &quot;SELECT * FROM Emloyee&quot;;
      
      //Act
      Task &lt;IEnumerable&lt;EmployeeDO&gt;&gt; elist = dbHelper.QueryMultipleAsync&lt;EmployeeDO&gt;(sqlCmd);
  
      //Assert
      Assert.True(elist != null);
  
      //foreach(var e in elist)
      //{
  
      //}
  
  }
  </code></pre>
  <p><strong>PROBLEM</strong></p>
  <p>Eventhough the test run successfully, I was unable to access the returned data that wrapped in the Task&lt;IEumerable&gt;.  After the Assert statement, I tried to interate the returned elist in the foreach loop (see the statements commented out above).  It failed. I also tried to use &quot;foreach(EmployeeDO e in elist)&quot; and got the same error.  The error message was the following:</p>
  <p><a href="https://i.stack.imgur.com/vuFEC.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vuFEC.jpg" alt="enter image description here" /></a></p>
  <p><strong>QUESTIONS</strong></p>
  <ol>
  <li><p>Am I using the correct return type with the DapperDbHelper.QueryMultipleAsync?</p>
  </li>
  <li><p>Can the QueryAsync method be used for retrieving a single record and multiple records?</p>
  </li>
  <li><p>What is the correct way of access returned data with type of Task&lt;IEnumerable&gt;?</p>
  </li>
  </ol>
  <p>I've tried to read many posts at the site and still unable to find the answer.  Any advice or assistance will be greatly appreciated.</p>