I'm trying to map multiple collections (one-to-many reference) for a single query into an object. The object is like the following:
public class Item {
public int Id { get; set; }
public string Name { get; set; }
public List<ItemDetail> Details { get; set; }
public List<ItemHistory> Histories { get; set; }
public List<ItemOrder> Orders { get; set; }
}
I've created a single query that returns me everything that I want, like the following:
SELECT
i.[Id], i.[Name],
detail.[Id] ItemDetailId, detail.[Description] ItemDetailDescription,
history.[Id] ItemHistoryId, history.[Date] ItemHistoryDate,
history.[Description] ItemHistoryDescription,
order.[Id] ItemOrderId, order.[Date] ItemOrderDate, order.[Quantity] ItemOrderQuantity
FROM
[dbo].[Items] i
inner join [dbo].[ItemDetails] detail on i.[Id] = detail.[ItemId]
inner join [dbo].[ItemHistories] history on i.[Id] = history.[ItemId]
inner join [dbo].[ItemOrders] order in i.[Id] = order.[ItemId]
Ok, I've added Dapper through Nuget and the Query
extension method is not allowing me to map multiple types in a row. How I can map the query above into the object above through Dapper.NET?
Thank y'all!
Dapper can split the query row by making an assumption that your Id columns are named Id/id (you can configure by splitOn parameter).
Additionally, you need to use QueryMultiple to collect your lists (ItemDetail...)