代码之家  ›  专栏  ›  技术社区  ›  Vishal

展平ObservableCollection

  •  1
  • Vishal  · 技术社区  · 8 年前

    我有一个这样的收藏:

    Order //Collection
     |-OrderId
     |-DateOfOrder
     |-PartyName
     |-OrderDetails //Collection
     |    |-ItemName
     |    |-Quantity
     |    |-Rate
     |    |-Amount
     |-Dispatch //Collection
     |    |-InvoiceNo
     |    |-DateOfDispatch
     |    |-DispatchDetails //Collection
     |    |    |-ItemName
     |    |    |-Quantity
     |    |    |-Rate
     |    |    |-Amount
    

    现在,我想展平这个集合,以便可以用下面提到的模式显示数据:

    OrderId | DateOfOrder | PartyName | InvoiceNo | DateOfDispatch | Dispatch ItemName | Dispatch Quantity | Dispatch Rate | Dispatch Amount
            |             |           |           |                |                   |                   |               |
            |             |           |           |                |                   |                   |               |
            |             |           |           |                |                   |                   |               |
            |             |           |           |                |                   |                   |               |
            |             |           |           |                |                   |                   |               |
            |             |           |           |                |                   |                   |               |
    

    我尝试过:

    Orders = new ObservableCollection<Order>(orderService.GetAllOrders()
                                              .SelectMany(x => x.Dispatches)
                                              .SelectMany(x => x.DispatchDetails)
                                              .ToList()
                                            );
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   Attila Karoly    8 年前

    我不清楚OrderDetails和DispatchDetails之间的关系,您的数据结构中似乎缺少DispatchItemTransactions。无论如何,我希望您觉得这个简单的方法有用:

    foreach(var order in Orders)
        foreach(var dispatch in order.Dispatches)
            foreach(var dispatchDetail in dispatch.DispatchDetails)
            {
                // now construct your record object from order.OrderId, order.DateOfOrder, ... , dispatchDetail.Amount
            }
    
        2
  •  2
  •   juharr    8 年前

    要使其发挥作用,您需要构建新的 Order Dispatch 物体。此外,查询语法将使其更易于阅读。

    Orders = new ObservableCollection<Order>(
        from o in orderService.GetAllOrders
        from d in o.Dispatches
        from dd in d.DispathDetails
        select new Order
        {
            OrderId = o.OrderId,
            DateOfOrder = o.DateOfOrder,
            PartyName = o.PartyName,
            Dispatches = new List<Dispatch> 
            { 
                new Dispatch
                {
                    InvoiceNo = d.InvoiceNo
                    DateOfDispatch = d.DateOfDispatch
                    DispatchDetails = new List<DispatchDetail> { dd }
                }
            }           
        });
    

    虽然不是收集 顺序 您可能只想使用匿名类

    from o in orderService.GetAllOrders
    from d in o.Dispatches
    from dd in d.DispathDetails
    select new 
    {
        OrderId = o.OrderId,
        DateOfOrder = o.DateOfOrder,
        PartyName = o.PartyName,
        InvoiceNo = d.InvoiceNo
        DateOfDispatch = d.DateOfDispatch,
        DispatchItemName = dd.ItemName,
        DispatchQuantity = dd.Quantity,
        DispatchRate = dd.Rate,
        DispatchAmount = dd.Amount
    }