简体   繁体   中英

Dapper selecting nested objects

My SQL looks like

Select TOP 3 o.OrderId,
o.Total,
od.Quantity,
od.ItemDescription,
os.OrderStatusDescription,
os.OrderStatusID
 From dbo.Orders o
 INNER JOIN dbo.OrderStatuses os ON os.OrderStatusID = o.OrderStatusID
 INNER JOIN dbo.OrderDetails od ON od.OrderID = o.OrderID
  Where o.CustomerId = 100 order by OrderDate desc;

I have my class on c# side as below, i have three tables on db side as well. i am trying to see if i can use dapper to map 3 tables into one object Order withouty having to do multiple select querys. i need top 3 rows from Order Table and its child tables loaded. OrderDetail can have n rows for 1 orderId.

How can i selected nested object with 1 look up using dapper?

 class Order{
      int OrderId {get;set;}      
      decimal Total {get;set;}
      List<OrderDetail> Details {get;set;}
      OrderStatus Status {get;set;}
 }

  class OrderDetail
  {
    int OrderLineId {get;set;}
    int OrderId {get;set;}  
    string ItemDescription {get;set;}
    int Quantity {get;set;}
  }

  class OrderStatus
  {
    int OrderStatusID {get;set;}
    string OrderStatusDescription {get;set;}
  }

this is what i have tried so far but not successful

  sqlConnection.Open();

IEnumerable orders = sqlConnection
    .Query<Order>(
        @"select Order.*, OrderDetail.* 
          from Order join OrderDetail 
               inner join on Order.OrderId = OrderDetail.OrderId
               inner join on Order.OrderStatusID = OrderStatus.OrderId
               and Order.CustomerId = 100",
        (o, od, os) =>
            {
                o.Details = od;  //this is a List<OrderDetail>
                o.Status = os;
                return o;
            }); // 

在此处输入图片说明

There are some ways to achieve your case, but for me you can do this by the below code

var sql = @"Select TOP 3 o.OrderId,
    o.Total,
    os.OrderStatusID,
    os.OrderStatusDescription,
    od.OrderLineId,
    od.Quantity,
    od.ItemDescription       
    From dbo.Orders o
INNER JOIN dbo.OrderStatuses os ON os.OrderStatusID = o.OrderStatusID
INNER JOIN dbo.OrderDetails od ON od.OrderID = o.OrderID
Where o.CustomerId = 100 order by OrderDate desc;";


sqlConnection.Open();

var lookupOrders = new Dictionary<int,Order>();
var oders = sqlConnection.Query<Order, OrderStatus, OrderDetails, Order>(sql,
               (o, os, od) => 
               {
                    Order order;
                    if(!lookupOrders.ContainsKey(o.OrderId)){
                        lookupOrders.Add(o.OrderId, o);                            
                        order = o;
                        order.Details = new List<OrderDetail>();
                    }
                    else {
                        order = lookupOrders[o.OrderId];
                    }
                    order.Details.Add(od);
                    order.Status = os;
                    return order;
               }, splitOn: "OrderId, OrderStatusID, OrderLineId"
            ).Distinct();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM