I got an error about the serialization of the object when the LINQ expression contained OrderLines = x.OrderLines Also it shouldn't be the OrderId but actually the OrderNumber I want to search for.
Now I have this, which returns basic info about the order and the an array of the orderlines. I'm not sure if it could be shorten or there is a way to return all properties in each OrderLine to strings? Right now I select some specific properties..
public string GetOrderDetails(string orderNumber)
{
var purchaseOrder =
PurchaseOrder.All().Single(x => x.OrderNumber == orderNumber && x.OrderStatus.OrderStatusId != 1);
var orderLines = new List<string[]>();
foreach (OrderLine ol in purchaseOrder.OrderLines)
{
var li = new string[]
{
ol.OrderLineId.ToLowerInvariantString(),
ol.Sku,
ol.ProductName,
ol.Price.ToLowerInvariantString(),
ol.Quantity.ToLowerInvariantString()
};
orderLines.Add(li);
}
var o = new List<object>
{
purchaseOrder.OrderId,
purchaseOrder.CreatedDate,
purchaseOrder.OrderStatus.OrderStatusId,
purchaseOrder.OrderStatus.Name,
orderLines
};
return JsonConvert.SerializeObject(o);
}
Return Order Details as json
Hi..
How can I return order details as json in via the API?
E.g. I can do this to return json about an order using Newtonsoft.Json:
public string GetOrderDetails(int orderId) { var order = PurchaseOrder.All() .Where(x => x.OrderStatus.OrderStatusId != 1 && x.OrderId == orderId) .Select(x => new { OrderId = x.OrderId, OrderDate = x.CreatedDate, OrderStatusId = x.OrderStatus.OrderStatusId, OrderStatus = x.OrderStatus.Name }) .FirstOrDefault(); return JsonConvert.SerializeObject(order); }However this won't work just to add the OrderLines:
public string GetOrderDetails(int orderId) { var order = PurchaseOrder.All() .Where(x => x.OrderStatus.OrderStatusId != 1 && x.OrderId == orderId) .Select(x => new { OrderId = x.OrderId, OrderDate = x.CreatedDate, OrderStatusId = x.OrderStatus.OrderStatusId, OrderStatus = x.OrderStatus.Name, OrderLines = x.OrderLines }) .FirstOrDefault(); return JsonConvert.SerializeObject(order); }/Bjarne
Do you get any errors?
I got an error about the serialization of the object when the LINQ expression contained OrderLines = x.OrderLines
Also it shouldn't be the OrderId but actually the OrderNumber I want to search for.
Now I have this, which returns basic info about the order and the an array of the orderlines. I'm not sure if it could be shorten or there is a way to return all properties in each OrderLine to strings? Right now I select some specific properties..
public string GetOrderDetails(string orderNumber) { var purchaseOrder = PurchaseOrder.All().Single(x => x.OrderNumber == orderNumber && x.OrderStatus.OrderStatusId != 1); var orderLines = new List<string[]>(); foreach (OrderLine ol in purchaseOrder.OrderLines) { var li = new string[] { ol.OrderLineId.ToLowerInvariantString(), ol.Sku, ol.ProductName, ol.Price.ToLowerInvariantString(), ol.Quantity.ToLowerInvariantString() }; orderLines.Add(li); } var o = new List<object> { purchaseOrder.OrderId, purchaseOrder.CreatedDate, purchaseOrder.OrderStatus.OrderStatusId, purchaseOrder.OrderStatus.Name, orderLines }; return JsonConvert.SerializeObject(o); }/Bjarne
As long as it works i guess you're good to go :)
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.