无法理解如何访问列表中的嵌套列表

Trouble understanding how to access nested list in list

我正在处理 xml 在项目中有采购订单的 soap 响应 例如

<PurchaseOrder>
<WHID>2</WHID>
<Supplier_ID>00</Supplier_ID>
<POID>6</POID>
<CreateDate>2013-01-02T10:48:27.37+11:00</CreateDate>
<CurrencyName>Australian Dollars</CurrencyName>
<ShippingStatus>Departed</ShippingStatus>
<payment_terms></payment_terms>
<shipping_terms></shipping_terms>
<POStatus>Back-Order</POStatus>
<PurchaseOrderItems>
<PurchaseOrderItem>
<SKU>Shoe30</SKU>
<Product_ID>124064</Product_ID>
<QtyOrdered>9</QtyOrdered>
<QtyOutstanding>6</QtyOutstanding>
<BuyPriceEx>20.0000</BuyPriceEx>
<DirectCosts>0.0000</DirectCosts>
<SupplierBuyPrice>20.0000</SupplierBuyPrice>
</PurchaseOrderItem>
</PurchaseOrderItems>
</PurchaseOrder>

我可以毫无疑问地将其放入锯齿状列表中。我的 class 看起来像这样

    public class PurchaseOrder
    {
        public string WHID { get; set; }
        public string Supplier_ID { get; set; }
        public string POID { get; set; }
        public string CreateDate { get; set; }
        public string CurrencyName { get; set; }
        public string ShippingStatus { get; set; }
        public string payment_terms { get; set; }
        public string shipping_terms { get; set; }
        public string POStatus { get; set; }
        public List<PurchaseOrderItems> PurchaseOrderItems { get; set; }
    }
    public class PurchaseOrderItems
    {
        public string SKU { get; set; }
        public string Product_ID { get; set; }
        public string QtyOrdered { get; set; }
        public string QtyOutstanding { get; set; }
        public string BuyPriceEx { get; set; }
        public string DirectCosts { get; set; }
        public string SupplierBuyPrice { get; set; }

    }

我使用以下 linq

填写采购订单 class
        List<PurchaseOrder> _orderDetailed = items.Select(po => new PurchaseOrder()
        {
            WHID = (string)po.Element("WHID").ElementValueNull(),
            Supplier_ID = (string)po.Element("Supplier_ID").ElementValueNull(),
            POID = (string)po.Element("POID").ElementValueNull(),
            CreateDate = (string)po.Element("CreateDate").ElementValueNull(),
            CurrencyName = (string)po.Element("CurrencyName").ElementValueNull(),
            payment_terms = (string)po.Element("payment_terms").ElementValueNull(),
            shipping_terms = (string)po.Element("shipping_terms").ElementValueNull(),
            POStatus = (string)po.Element("POStatus").ElementValueNull(),
            PurchaseOrderItems = po.Descendants("PurchaseOrderItem").Select(i => new PurchaseOrderItems()
            {
                SKU = (string)i.Element("SKU").ElementValueNull(),
                Product_ID = (string)i.Element("Product_ID").ElementValueNull(),
                QtyOrdered = (string)i.Element("QtyOrdered").ElementValueNull()
            }).ToList()

        }).ToList();

当我将其传递给将 object 写入 csv 的反射函数时,问题就来了。它只将 PurchaseOrder 字段写入文件。我不知道如何访问 PurchaseOrderItems 字段以便将它们写入文件。

我需要使用上面的 xml 结构实现以下目标。

WHID Supplier_ID POID SKU    Product_ID QtyOrdered
2    00          6    Shoe30 124064     6

为了便于阅读,我删减了上面的字段。但目标是将所有订单项和采购订单 header 详细信息放在一行中。

public void WriteCSV<T>(IEnumerable<T> items, string path)
{
    Type itemType = typeof(T);
    var props = itemType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                        .OrderBy(p => p.Name);

    using (var writer = new StreamWriter(path))
    {
        writer.WriteLine(string.Join(fieldDelimiter, props.Select(p => p.Name)));

        foreach (var item in items)
        {
            writer.WriteLine(string.Join(fieldDelimiter, props.Select(p => p.GetValue(item, null))));
        }
    }
}

我知道我想念 object 在这里的工作方式,所以正在寻找方向。

非常感谢。

无需在 WriteCSV 中处理它,您可以 pre-process 将数据展平(非规范化),然后将其传递给您现有的 WriteCSV:

var flatten = l.SelectMany(po => po.PurchaseOrderItems.Select(pi => new {
    po.WHID,
    po.Supplier_ID,
    po.POID,
    pi.SKU,
    pi.Product_ID,
    pi.QtyOrdered,
}));

WriteCSV(flatten);