title: How to turn dapper result into a dictionary using result mapping
body: <p>I want to use the splitOn feature denoted here: <a href="https://dapper-tutorial.net/result-multi-mapping" rel="noreferrer">https://dapper-tutorial.net/result-multi-mapping</a></p>
  
  <p>to group every Order of the results to a integer property "EmployeeId". I Followed the advice from <a href="https://stackoverflow.com/questions/14780767/how-to-map-to-a-dictionary-object-from-database-results-using-dapper-dot-net">How to map to a Dictionary object from database results using Dapper Dot Net?</a></p>
  
  <p>but I am getting a <code>An item with the same key has already been added.</code> so how can I group my orders by EmployeeId? </p>
  
  <p>I cannot modify the <code>Order</code> class and I prefer using a dictionary over creating a class that wraps Order. However, if there is no other way I am open to the idea of wrapping Order</p>
  
  <p><a href="https://dotnetfiddle.net/hn6Sjf" rel="noreferrer">https://dotnetfiddle.net/hn6Sjf</a></p>
  
  <pre><code>public class Program
  {
      public class Order
      {
          public int OrderID { get; set; }
          public int CustomerID { get; set; }
          public DateTime OrderDate  { get; set; }
          public int ShipperID  { get; set; }
      }
  
      public static void Main()
      {
          string sql = @"
              SELECT TOP 10
                  EmployeeID,
                  OrderID,
                  CustomerID,
                  OrderDate,
                  ShipperID
              FROM Orders 
              ORDER BY OrderID;
          ";
  
          using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
          {           
              var rawList = connection.Query&lt;Order&gt;(sql);
              FiddleHelper.WriteTable(rawList);   
  
  
                  var dict = connection.Query&lt;int, List&lt;Order&gt;, KeyValuePair&lt;int, List&lt;Order&gt;&gt;&gt;(sql,
                      (s, i) =&gt; new KeyValuePair&lt;int, List&lt;Order&gt;&gt;(s, i), null, null, true, "OrderID")
                      .ToDictionary(kv =&gt; kv.Key, kv =&gt; kv.Value);
  
              FiddleHelper.WriteTable(dict);              
          }
      }
  }
  </code></pre>