Say I have a list of OrderItems like so:
public class OrderItem
{
public long Id { get; set; }
public long OrderNo { get; set; }
public string OrderEvent { get; set; }
public string OrderData { get; set; }
}
Each item is being added to a SQL database as an OrderEvent comes through with the OrderData being stored as JSON. The OrderNo is the unique identifier. Currently in is inserting 3 rows into the db and I would like to consolidate it into a single row based on the OrderNo and save the OrderEvent and OrderData in a single JSON field.
How can I rollup all the items into one item with the OrderData containing a JSON array with each OrderEvent and OrderData from each item?
So once rolled up, each OrderData will be stored as JSON like so:
[
{
orderEvent: "Start",
orderData: {
orderLine1: "line1",
orderLine2: "line2",
}
},
{
orderEvent: "Collected",
orderData: {
orderCollectedLine1: "line1",
orderCollectedLine2: "line2",
}
},
{
orderEvent: "End",
orderData: {
orderEndLine1: "line1",
orderEndLine2: "line2",
}
}
]
I have tried using groupBy on the OrderNo but it just gives me all the items. Not sure if I should be using a Select with this.
var data = orders.GroupBy(x => x.OrderNo );
how can you rollupthe only answer isby rolling them upunfortunately