title: Multi-Mapper to create object hierarchy
body: <p>I've been playing around with this for a bit, because it seems like it feels a lot like the <a href="http://code.google.com/p/dapper-dot-net/" rel="noreferrer">documented posts/users example</a>, but its slightly different and isn't working for me.</p>
  
  <p>Assuming the following simplified setup (a contact has multiple phone numbers):</p>
  
  <pre><code>public class Contact
  {
      public int ContactID { get; set; }
      public string ContactName { get; set; }
      public IEnumerable&lt;Phone&gt; Phones { get; set; }
  }
  
  public class Phone
  {
      public int PhoneId { get; set; }
      public int ContactID { get; set; } // foreign key
      public string Number { get; set; }
      public string Type { get; set; }
      public bool IsActive { get; set; }
  }
  </code></pre>
  
  <p>I'd love to end up with something that returns a Contact with multiple Phone objects.  That way, if I had 2 contacts, with 2 phones each, my SQL would return a join of those as a result set with 4 total rows.  Then Dapper would pop out 2 contact objects with two phones each.</p>
  
  <p>Here is the SQL in the stored procedure:</p>
  
  <pre><code>SELECT *
  FROM Contacts
      LEFT OUTER JOIN Phones ON Phones.ReferenceId=Contacts.ReferenceId
  WHERE clientid=1
  </code></pre>
  
  <p>I tried this, but ended up with 4 Tuples (which is OK, but not what I was hoping for... it just means I still have to re-normalize the result):</p>
  
  <pre><code>var x = cn.Query&lt;Contact, Phone, Tuple&lt;Contact, Phone&gt;&gt;("sproc_Contacts_SelectByClient",
                                (co, ph) =&gt; Tuple.Create(co, ph), 
                                            splitOn: "PhoneId", param: p, 
                                            commandType: CommandType.StoredProcedure);
  </code></pre>
  
  <p>and when I try another method (below), I get an exception of "Unable to cast object of type 'System.Int32' to type 'System.Collections.Generic.IEnumerable`1[Phone]'."</p>
  
  <pre><code>var x = cn.Query&lt;Contact, IEnumerable&lt;Phone&gt;, Contact&gt;("sproc_Contacts_SelectByClient",
                                 (co, ph) =&gt; { co.Phones = ph; return co; }, 
                                               splitOn: "PhoneId", param: p,
                                               commandType: CommandType.StoredProcedure);
  </code></pre>
  
  <p>Am I just doing something wrong? It seems just like the posts/owner example, except that I'm going from the parent to the child instead of the child to the parent.</p>
  
  <p>Thanks in advance</p>