title: Using Async and Await to break up database call (with Dapper)
body: <p>We're requesting thousands of objects back from Dapper and hit the parameter limit (2100) so have decided to load them in chunks.</p>
  
  <p>I thought it would be a good opportunity to try Async Await - this the first time I've had a go so maybe making a school boy error!  </p>
  
  <p>Breakpoints are being hit, but the whole thing is just not returning.  It's not throwing an error - it just seems like everything is going in a black hole!</p>
  
  <p>Help please!</p>
  
  <p>This was my original method - it now calls the Async method </p>
  
  <pre><code>    public List&lt;MyObject&gt; Get(IEnumerable&lt;int&gt; ids)
      {
          return this.GetMyObjectsAsync(ids).Result.ToList();
      }  //Breakpoint on this final bracket never gets hit
  </code></pre>
  
  <p>I added this method to split the ids into chunks of 1000 and then await for the tasks to complete</p>
  
  <pre><code>    private async Task&lt;List&lt;MyObject&gt;&gt; GetMyObjectsAsync(IEnumerable&lt;int&gt; ids)
      {
          var subSets = this.Partition(ids, 1000);
  
          var tasks = subSets.Select(set =&gt; GetMyObjectsTask(set.ToArray()));
  
          //breakpoint on the line below gets hit ...
          var multiLists = await Task.WhenAll(tasks);
  
          //breakpoint on line below never gets hit ...
          var list = new List&lt;MyObject&gt;();
          foreach (var myobj in multiLists)
          {
              list.AddRange(myobj);   
          }
          return list;
      }
  </code></pre>
  
  <p>and below is the task ...</p>
  
  <pre><code>    private async Task&lt;IEnumerable&lt;MyObject&gt;&gt; GetMyObjectsTask(params int[] ids)
      {
          using (var db = new SqlConnection(this.connectionString))
          {
              //breakpoint on the line below gets hit
              await db.OpenAsync();
              return await db.QueryAsync&lt;MyObject&gt;(@"SELECT Something FROM Somewhere WHERE ID IN @Ids",
              new { ids});
          }
      }
  </code></pre>
  
  <p>The following method just splits the list of ids in chunks - this appears to work fine ...</p>
  
  <pre><code>    private IEnumerable&lt;IEnumerable&lt;T&gt;&gt; Partition&lt;T&gt;(IEnumerable&lt;T&gt; source, int size)
      {
          var partition = new List&lt;T&gt;(size);
          var counter = 0;
  
          using (var enumerator = source.GetEnumerator())
          {
              while (enumerator.MoveNext())
              {
                  partition.Add(enumerator.Current);
                  counter++;
                  if (counter % size == 0)
                  {
                      yield return partition.ToList();
                      partition.Clear();
                      counter = 0;
                  }
              }
  
              if (counter != 0)
                  yield return partition;
          }
      }
  </code></pre>