DapperLib / Dapper

Dapper - a simple object mapper for .Net
https://www.learndapper.com/
Other
17.48k stars 3.67k forks source link

How I can read Parent - Child DataSet into object structure (If it possible) #1151

Open IFetisov opened 5 years ago

IFetisov commented 5 years ago

Hello. I'm beginner and don't know fuctionality of this great component. Can someone help me with reading data to object model

    public class Order
    {
        public int OrderId { get; set; }
        public DateTime Date { get; set; }
        public List<Detail> Details { get; set; }
    }
    public class Detail
    {
        public int OrderId { get; set; }
        public string Product { get; set; }
    }
SELECT O.OrderId, O.DateTime, D.Product
FROM Orders O
INNER JOIN Details D ON D.OrderId=O.OrderId

May be I should do so? var x = multi.Read<Order, Detail, Order>((o, d) => { o.Details.Add(d); return o; }, splitOn:"OrderId"); But it returns in collection duplicate records

tuan-tu-tran commented 5 years ago

Hello,

This example may help you: https://github.com/doc212/dapper-issue-1151/blob/master/Program.cs

It's taken mostly from here (One to Many) https://dapper-tutorial.net/result-multi-mapping

If it does not help you, could you clarify what you mean by :

But it returns in collection duplicate records

And maybe provide a more complete example

Also I think this kind of question fits better in StackOverflow (but that's just my opinion)

Cheers.