Vishal
Vishal

Reputation: 6368

Flatten an ObservableCollection

I have a collection like this:

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

Now I want to flatten this collection, so that I can show data in below mentioned pattern:

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

I have tried:

Orders = new ObservableCollection<Order>(orderService.GetAllOrders()
                                          .SelectMany(x => x.Dispatches)
                                          .SelectMany(x => x.DispatchDetails)
                                          .ToList()
                                        );

Upvotes: 1

Views: 385

Answers (2)

juharr
juharr

Reputation: 32276

For this to work you'll need to construct new Order and Dispatch objects. Also query syntax will make this much easier to read.

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 }
            }
        }           
    });

Though instead of a collection of Order you might want to just use an anonymous class instead

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
}

Upvotes: 2

Attila Karoly
Attila Karoly

Reputation: 1021

The relation between OrderDetails and DispatchDetails is not clear to me, and DispatchItemTransactions seems to be missing from your data structure. Anyway, I hope you find this simple approach useful:

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
        }

Upvotes: 2

Related Questions