公共类中的访问数组列表

时间:2019-04-15 21:33:15

标签: c# linq class

不是重复的 Add objects to arraylist and read them 我可以访问数据并以json格式读取它们,但不能以orderItem.PO等格式读取它们。而且我的问题不是“如何阅读”。

我有一个List<xmlOrder>物品,其中包含订单信息及其物品。 我可以访问以下列表:

    foreach (var order in items)
    {
       string query = "insert into Orders values ('" + order.PO + "','" + order.Name + "','" + order.Company + "','" + order.Address + "','" + order.City + "','" + order.State + "','" + order.ZipCode + "','" + order.Country + "','" + order.Phone + "','" + order.Date + "','" + order.ShippingMethod + "','" + order.Notes + "')";
       foreach (var orderItem in order.OrderItems)
       {
        query = "insert into OrderItem values ('" + orderItem.PO + "','" + orderItem.StockNumber + "','" + orderItem.Quantity + "','" + orderItem.UnitPrice + "')";
       //Console.WriteLine(orderItem.ToJson());
       }
    }

我可以访问order.PO,order.Name等,但是我收到了orderItem的错误,如下所示: enter image description here

奇怪的是,当我检查orderItem.ToJson()时,得到的json很好,如:

{"PO":"009723","StockNumber":"0040","Quantity":5,"UnitPrice":16.445000}

这意味着我正在获取数据,但是为什么当我尝试访问诸如orderItem.PO之类的数据时却给我错误?

如果需要,我的班级结构如下:

public class xmlOrder
{

    public string PO { get; set; }
    public string Name { get; set; }
    public string Company { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }
    public string Phone { get; set; }
    public string Date { get; set; }
    public string ShippingMethod { get; set; }
    public string Notes { get; set; }
    public ArrayList OrderItems { get; set; }

    public xmlOrder()
    {
        this.PO = string.Empty;
        this.Name = string.Empty;
        this.Company = string.Empty;
        this.Address = string.Empty;
        this.City = string.Empty;
        this.State = string.Empty;
        this.ZipCode = string.Empty;
        this.Country = string.Empty;
        this.Phone = string.Empty;
        this.Date = string.Empty;
        this.ShippingMethod = string.Empty;
        this.Notes = string.Empty;
        this.OrderItems = new ArrayList();
    }
}

public class xmlOrderItem
{
    public string PO { get; set; }
    public string StockNumber { get; set; }
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }

    public xmlOrderItem()
    {
        this.PO = string.Empty;
        this.StockNumber = string.Empty;
        this.Quantity = 0;
        this.UnitPrice = decimal.Zero;
    }
}

1 个答案:

答案 0 :(得分:1)

您的items对象的类型是什么?您应该展示更多这种方法。如果类型为object,则应尝试使其成为一个具体对象。您还可以将object强制转换为您的类型:

foreach (var orderItem in order.OrderItems)
   {
      var item = orderItem as xmlOrderItem
      if (item == null)
        throw new NullReferenceException("orderItem is not an xmlOrderItem")
      query = "insert into OrderItem values ('" + item .PO + "','" + item .StockNumber + "','" + item .Quantity + "','" + item .UnitPrice + "')";
   //Console.WriteLine(orderItem.ToJson());
   }