0

So I got a collection of type Item and I'm trying to map the values to a CSV file but it's not working properly.. It maps these values just fine.. ItemName SubTitle Price Condition Quantity QuantitySold ProductImage and then it doesn't map any more. I think this is because the other two properties are collections.. List and Dictionary

This is how I am currently mapping it.

using (var mem = new MemoryStream())
using (var writer = new StreamWriter(mem))
using (var csvWriter = new CsvWriter(writer, CultureInfo.CurrentCulture))
{
    csvWriter.Configuration.Delimiter = ",";
    csvWriter.Configuration.HasHeaderRecord = true;
    csvWriter.Configuration.AutoMap<Item>();

    csvWriter.WriteHeader<Item>();
    csvWriter.WriteRecords(Products);

    writer.Flush();
    var result = Encoding.UTF8.GetString(mem.ToArray());
    File.WriteAllText("Items.csv", result);
    Console.WriteLine(result);
}

And here is the model

class Item
{
    public string ItemName { get; set; }
    public string SubTitle { get; set; }
    public string Price { get; set; }
    public string Condition { get; set; }
    public Dictionary<string, List<string>> SKU { get; set; }
    public string Quantity { get; set; }
    public string QuantitySold { get; set; }
    public string ProductImage { get; set; }
    public List<string> ProductImages { get; set; }
}
Riley Varga
  • 670
  • 1
  • 5
  • 15
  • Collections are not auto mapped. There are ways to write out collections. However, your model looks a bit complicated to be putting into a CSV file. What do you want the result to look like? You could try here for some ideas. https://github.com/JoshClose/CsvHelper/issues/891 – David Specht Jun 11 '20 at 04:50
  • Does this answer your question? [How do i make a sublist of every CSV row and put that sublist inside a list](https://stackoverflow.com/questions/55477180/how-do-i-make-a-sublist-of-every-csv-row-and-put-that-sublist-inside-a-list) – habib Jun 11 '20 at 06:39

0 Answers0