Filling aggregate class from dataTable using a Dataset with LINQ C #

7

I'm having trouble filling an aggregate class in C #. Is it possible using LINQ? Does anyone have any examples of how to work around this problem using the return of a DataTable or DataSet ?

public class OrderDetails
{
 . . .
}

public class Order
{
     private List<OrderDetails> details;
     public Person(OrderDetails pdetails)
     {
         this.details.Add(pdetails);
     }
     . . .
}
    
asked by anonymous 20.05.2015 / 05:43

1 answer

3

You will need to iterate through each DataTable result and group the information of each Order into a dictionary indexed by your Id, so that you can aggregate the OrderDetails in the same Order 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 through the DataSet results and populate the class. I will assume some premises:

1) that the Order Id is a String. This premise is based only on the fact that it is easier to deal with String in the example. Make the necessary conversions for 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 first 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;
}
You asked if it was possible to populate classes with linq, 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 linq. 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 you research EF and try to implement an access to your bank using it. The documentation is extensive and there is also material in Portuguese. If you have any questions, please ask another question in StackOverflow so that it does not become too large.

    
21.05.2015 / 04:47