I checked previous threads, could not find anything..
The scenario is, 3 classes :
public class Customer
{
public long Cust_id { get; set; }
public string Cust_name { get; set; }
public List<Order> Orders { get; set; }
}
public class Order
{
public long Order_id { get; set; }
public long Cust_id { get; set; }
public string Order_no { get; set; }
public List<OrderDetail> OrderDetails { get; set; }
}
public class OrderDetail
{
public long Odetail_id { get; set; }
public long Order_id { get; set; }
public decimal Final_price { get; set; }
}
and running this :
string sqlQuery = @"select customers.* , orders.*, order_detail.* from customers
inner join orders on orders.cust_id = customers.cust_id
inner join order_detail on order_detail.order_id = orders.order_id";
//here we getting raw the data *1* multiple cust_id, *2* multiple order_id, *3* multiple order_detail.order_id
var t = General.db.GetConnection().Query<Customer, Order, OrderDetail, Customer>(sqlQuery, (customer, order, orderdetail) =>
{
//1st rel
customer.Orders = new List<Order>();
customer.Orders.Add(order);
//2nd rel
customer.Orders[0].OrderDetails = new List<OrderDetail>();
customer.Orders[0].OrderDetails.Add(orderdetail);
return customer;
}, splitOn: "order_id, odetail_id");
which return exactly record count like this (sample query on database) :
using the
var result = t.GroupBy(p => p.Cust_id).Select(g =>
{
var groupedCustomer = g.First();
groupedCustomer.Orders = g.Select(p => p.Orders.Single()).ToList();
return groupedCustomer;
});
found here, is able to group the ORDERS by CUSTOMERS - OK... But we need also the to group the ORDERDETAILS by ORDER... Anyone knows something or alternative way than LINQ ?