How I can read Parent - Child DataSet into object structure (If it possible)
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
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.
Hi Everyone I am working on a Dynamic Query Builder logic using Dapper. For One to One Relationship
dynamic result ;
dynamic ParentObject = new ExpandoObject();
dynamic ChildObject = new ExpandoObject();
ParentObject.ChildObject = ChildObject;
using (var conn = new SqlConnection(_CONN_STRING))
{
string sql = @"Select txtMatterName,
dtpMatterReportedDate,
txtDescription,
TeamName,
Teams.Name from matter_creation left join Teams on TeamName = Teams.Id";
result = conn.Query<dynamic, dynamic, dynamic>(sql, (parent, child) =>
{
parent.ChildObject = child;
return parent;
},null, splitOn: "TeamName");
}
this gave me Parent with one child option of one to one using dynamic object.
var ParentCollection = new Dictionary<long,dynamic>();
ParentObject = new ExpandoObject();
ParentObject.Child = new List<dynamic>();
using (var conn = new SqlConnection(_CONN_STRING))
{
string sql = @"SELECT Teams.[Id]
,[Name]
,[Description]
,[NumberOfMember], Matter_Creation.TeamName, Matter_Creation.txtMatterName
FROM [DB_LCF_SQL].[dbo].[Teams] left join Matter_Creation on Teams.Id =Matter_Creation.TeamName ";
result = conn.Query<dynamic, dynamic, dynamic>(sql, (parent, child) =>
{
if (ParentCollection.TryGetValue(parent.Id,out dynamic existingParent))
{
parent = existingParent;
}
else
{
ParentCollection.Add(parent.Id, parent);
}
if (parent.Child == null)
parent.Child = new List<dynamic>();
parent.Child.Add(child);
return parent;
}, null, splitOn: "TeamName");
}
var j = ParentCollection;
this gave me one to many parent and child data collection in dictionary. above code works perfectly for me. I am having a question is there is any way to assign the number of dynamic object run time . "conn.Query<dynamic, dynamic, dynamic>(sql, (parent, child) =>" in this line i gave dynamic, dynamic, dynamic helps me to build parent and child but based on the relationship key available in a table, is there is a way to change the number of dynamic object parameter runtime .