展平ObservableCollection

时间:2016-06-20 12:04:12

标签: c# wpf linq

我有一个这样的集合:

if($image_name==''){
echo "You forgot to select an image. Please choose one!";
exit();
}
else { // add braces for else part
move_uploaded_file($image_tmp_name, "MyMap/MyPhotoMap/$image_name"); 
echo "Image Succesfully Uploaded!";
}

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

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
        |             |           |           |                |                   |                   |               |
        |             |           |           |                |                   |                   |               |
        |             |           |           |                |                   |                   |               |
        |             |           |           |                |                   |                   |               |
        |             |           |           |                |                   |                   |               |
        |             |           |           |                |                   |                   |               |

2 个答案:

答案 0 :(得分:2)

为此,您需要构建新的OrderDispatch对象。查询语法也会使这更容易阅读。

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

虽然代替Order的集合,但您可能只想使用匿名类

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
}

答案 1 :(得分:2)

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
        }