I have to produce this CSV layout, but a little unsure how to do it.
I have a Order class that I have to convert into a CSV file.
public class Order
{
public int ID {get; set;}
public int Created {get;set;}
public List<Item> Items {get;set;}
}
public class Item
{
public string Sku {get;set;}
public int Quantity {get;set;}
}
So each row represents a order. If an order has more than 2 Items in it, I will have to continue to the next line leaving everything blank except for the sku, quanity and a specifical field.
The last field will be "Y" if its continuing from the previous line, "N" otherwise.
So I have:
List<Order> orders = Db.GetOrders();
So a sample output of the CSV would be:
"orderID", "Created", "Item01", "quantity01", "Item02", "N"
if there are more than 2 items, it has to output:
"orderID", "Created", "Item01", "quantity01", "Item02", "N"
"", "", "Item03", "quantity03", "", "", "Y"
So the above order had 3 items, notice how the placeholder for Item#4 is still present, but just empty double quotes.
So every field is mandatory.
How would I produce this?
So far I have:
StringBuilder sb = new StringBuilder();
for(int x = 0; x < orders.Count; x++)
{
Order o = orders[x];
if(x % 2 = 0)
{
sb.Append(o.ID).Append(",");
sb.Append(o.Created).Append(",");
}
}
The trick I guess is to figure out if I need to continue to the next line, and then I have to fill any empty spots if I have less than 2 items in the row?
(you can ignore any bad characters in the output, I'll deal will that later thanks)