I'm struggling with a query that returns a parent row and a variable number of child rows in a single flat result set. Dapper is great for simple 1:1 mappings, but here I need to group children under their parent dynamically.
For example, a Departments table joined to Employees where each department has zero or more employees. I don't want to run N+1 queries. My first thought was to use multiple result sets, but my SQL only returns one.
What patterns do people use? I've seen references to custom multi-mapping with Query<T, U, T>, but the syntax looks intimidating. Is there a cleaner way, perhaps by splitting the flat list in memory after a single query?
If someone could share a minimal working example of splitting a flat Dapper result into a parent-child hierarchy, that would save me hours of trial and error.
Can you answer this question?
Write Answer0 Answers