Mapping Dapper Query to a Collection of Objects (which itself has a couple of Collections)
c#, dapper, multi-mapping
Solution
Not sure if you DON'T want to use MultiMapping but here's how it would work for your case. As far as I know and read on SO, is not not possible to map a deep nested object graph with a simple `Query`.
static void Main(string[] args)
{
var sqlParent = "SELECT parentId as Id FROM ParentTable WHERE parentId=1;";
var sqlChildOneSet = "SELECT Name FROM ChildOneTable;"; // Add an appropriate WHERE
var sqlChildTwoSet = "SELECT Id, LocationName FROM ChildTwoTable;"; // Add an appropriate WHERE
var conn = GetConnection() // whatever you're getting connections with
using (conn)
{
conn.Open();
using (var multi = conn.QueryMultiple(sqlParent + sqlChildOneSet + sqlChildTwoSet))
{
var parent = multi.Read<ParentObject>().First();
parent.ChildSetOne = multi.Read<ChildOne>().ToList();
parent.ChildSetTwo = multi.Read<ChildTwo>().ToList();
}
}
}
Similar questions for nested objects and dapper :
https://stackoverflow.com/search?q=nested+objects+%2B+dapper
Problem
I want to execute a single Query (or Stored Proc with multiple resultsets). I know how to do Multi-mapping using Dapper, but I can't sort how to map the two collections onto the same parent. Basically, given this Object definition... ``` class ParentObject { string Name { get; set; } ICollection<ChildObjectOne> ChildSetOne {get;set;} ICollection<ChildObjectTwo> ChildSetTwo { get; set; } } class ChildObjectOne { string Name { get; set; } } class ChildObjectTwo { int id { get; set; } string LocationName { get; set; } } ``` I want to be able to run a Dapper query that somehow yields: ``` IQueryable<ParentObject> result = cnn.Query( // Some really awesome dapper syntax goes here ); ```