You will need to iterate on each Datatable result and group the information of each Order in a dictionary indexed by your Id, in order to be able to aggregate the Order Orderdetails in the same instances.
The first step is to make the details list public:
public class Order
{
//Precisa ser público, já que a modelagem sugere que um Order
//pode ter mais de um OrderDetails. Precisaremos adicionar
//mais detalhes ao iterar nas linhas do DataSet.
public List<OrderDetails> Details { get; set; }
//Mantive o OrderDetail sendo passado no construtor, já que o
//modelo explicita que um Order deve possuir ao menos um OrderDetail
public Order(OrderDetails detail)
{
this.Details = new List<OrderDetails>();
this.Details.Add(detail);
}
}
Now we can iterate on the Dataset results and fill the class.
I’ll assume some premises:
1) that the Order Id is a String. This premise is based only on the fact that it is easier to handle String in the example. Make the necessary conversions to your code.
2) that your Dataset has already been configured and a Datatable has already been created from it. If this premise is false, please search before on how to access DB data via Dataset:
private IEnumerable<Order> GetOrdersFromDataTable(DataTable ordersTable)
{
var orderDict = new Dictionary<string, Order>();
foreach (DataRow dr in ordersTable.Rows)
{
var orderDetail = new OrderDetail();
//Todo: Preencher as propriedades do OrderDetail.
string id = dr["Id"].ToString());
if (!orderDict.ContainsKey(id))
{
var order = new Order(orderDetail);
//Todo: Preencher as propriedades do Order.
orderDict.Add(id, order);
}
else
orderDict[id].Details.Add(orderDetail);
}
return orderDict.Values;
}
Editing: You asked if it was possible to fill the classes with Linq2sql, but I believe you meant Linq2sql, which is a tool for abstracting access to the data source, mapping tables into classes and allowing queries using Linq2sql. There is another library, however, called Entity Framework, more robust than the previous one, which came to replace it. If you are interested in learning more, I suggest researching EF and trying to implement an access to your bank using it. The documentation is extensive and there is material even in Portuguese. If you have any questions, ask another question on Stackoverflow, so it doesn’t become too wide.
Your intention is also unclear. Do you have an Order class and want to add Orderdetails to it? How do you want the addition to be done? Via UI? Or is this a class that will be populated in a data model and returned to the client?
– Gabriel Rainha
So, I have a method that returns a Datatable, I would like to fill this List<Oderdetails> of the Order Class, that is, bringing a collection of Orderdetails. For example in my SQL I am using "SELECT O.Id, O.date, O.person, O.value, D.* FROM Order INNER JOIN Orderdetails ON O.id = D.fk_Order", as I do to pass the data to the Orderdetails collection of the Order Class?
– ts.analista
Okay, now I get it. I advise you to change your question to something like "fill aggregate class from datatable". I’ll give you an answer in a minute.
– Gabriel Rainha
Okay.. I changed the title
– ts.analista
Just one more tip: add the query used in Dataset to the question. Future people who have the same doubt may not understand my implementation right without seeing the data source, and a comment is not the best place for an important problem data.
– Gabriel Rainha