0

I have this Model which I am trying to fill with data from DataTable so I can serialize the model to JSON. However, I can't seem to find the correct way to fill in the order line items into the OrderDetails Class which is listed by OrderDetailsList.

So far, this is what I have.

Any help is greatly appreciated.

Thank you!

Code

List<Order> orders = new List<Order>();

orders = (from DataRow order in DTOrders.Rows
          select new Order()
          {
               ClientPO = "123456",
               MerchantOrderID = "123456",
               OrderDetailsList = (from DataRow orderDetail in DTOrderItems.AsEnumerable()
               where orderDetail["Order Number"].ToString() == order["Order Number"].ToString()
               select new OrderItem()
               {
                    //ClientJobRefDetail etc...
               }).ToList()
          }).ToList();

Model

    public partial class OrderModel
    {
        [JsonProperty("Order")]
        public Order Order { get; set; }
    }

    public partial class Order
    {
        [JsonProperty("ClientPO")]
        [JsonConverter(typeof(ParseStringConverter))]
        public long ClientPo { get; set; }

        [JsonProperty("MerchantOrderID")]
        [JsonConverter(typeof(ParseStringConverter))]
        public long MerchantOrderId { get; set; }

        [JsonProperty("OrderDetailsList")]
        public OrderDetailsList OrderDetailsList { get; set; }
    }

    public partial class OrderDetailsList
    {
        [JsonProperty("OrderDetails")]
        public List<OrderDetail> OrderDetails { get; set; }
    }

    public partial class OrderDetail
    {
        [JsonProperty("ClientJobRefDetail")]
        public string ClientJobRefDetail { get; set; }

        [JsonProperty("Product")]
        [JsonConverter(typeof(ParseStringConverter))]
        public long Product { get; set; }

        [JsonProperty("Qty")]
        [JsonConverter(typeof(ParseStringConverter))]
        public long Qty { get; set; }

        [JsonProperty("ShipMethod")]
        [JsonConverter(typeof(ParseStringConverter))]
        public long ShipMethod { get; set; }

        [JsonProperty("ShipTo")]
        public string ShipTo { get; set; }

        [JsonProperty("ShipToPerson")]
        public string ShipToPerson { get; set; }

        [JsonProperty("ShipToAddress1")]
        public string ShipToAddress1 { get; set; }

        [JsonProperty("ShipToCity")]
        public string ShipToCity { get; set; }

        [JsonProperty("ShipToState")]
        public string ShipToState { get; set; }

        [JsonProperty("ShipToZip")]
        [JsonConverter(typeof(ParseStringConverter))]
        public long ShipToZip { get; set; }

        [JsonProperty("ShipToPhone")]
        public string ShipToPhone { get; set; }

        [JsonProperty("ShipToEmail")]
        public string ShipToEmail { get; set; }

        [JsonProperty("ExternalArtURL")]
        public Uri ExternalArtUrl { get; set; }
    }

Desired Output after class is serialized into JSON

{
  "Order": {
    "ClientPO": "999999",
    "MerchantOrderID": "123456",
    "OrderDetailsList": {
      "OrderDetails": [
    {
      "ClientJobRefDetail": "LINE123456",
      "Product": "369",
      "Qty": "1",
      "ShipMethod": "60",
      "ShipTo": "John Doe",
      "ShipToPerson": "John Doe",
      "ShipToAddress1": "120 9th Ave",
      "ShipToCity": "Longmont",
      "ShipToState": "CO",
      "ShipToZip": "80501",
      "ShipToPhone": "5055555555",
      "ShipToEmail": "[email protected]",
      "ExternalArtURL": "http://example.com/print_ready_image.jpg"
    },
    {
      "ClientJobRefDetail": "LINE78910",
      "Product": "521",
      "Qty": "2",
      "ShipMethod": "60",
      "ShipTo": "John Doe",
      "ShipToPerson": "John Doe",
      "ShipToAddress1": "120 9th Ave",
      "ShipToCity": "Longmont",
      "ShipToState": "CO",
      "ShipToZip": "80501",
      "ShipToPhone": "5055555555",
      "ShipToEmail": "[email protected]",
      "ExternalArtURL": "http://example.com/print_ready_image.jpg"
    }
      ]
    }
  }
}

1 Answer 1

1

Need to create a new OrderDetailsList

orders = (from DataRow order in DTOrders.Rows
select new Order()
{
    ClientPo = 123456,
    MerchantOrderId = 123456,
    OrderDetailsList = new OrderDetailsList()
    {
        OrderDetails = (from DataRow orderDetail in DTOrderItems.AsEnumerable()
                        where orderDetail["Order Number"].ToString() == order["Order Number"].ToString()
                        select new OrderDetail()
                        {
                            //ClientJobRefDetail etc...
                        }).ToList()
    }
}).ToList();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.