using System.Collections.Generic;
using System.Globalization;
using CsvHelper.Configuration;
public static void Main()
new Bar { BarID = "A", BarProperty1 = "Red", BarProperty2 = "Red", BarProperty3 = "Green" },
new Bar { BarID = "A", BarProperty1 = "Green", BarProperty2 = "Green", BarProperty3 = "Red" },
new Bar { BarID = "B", BarProperty1 = "Red", BarProperty2 = "Purple", BarProperty3 = "Orange" },
new Bar { BarID = "C", BarProperty1 = "White", BarProperty2 = "Black", BarProperty3 = "Red" },
using (var memory = new MemoryStream())
using (var writer = new StreamWriter(memory))
using (var csv = new CsvWriter(writer, CultureInfo.GetCultureInfo("en")))
csv.Context.RegisterClassMap<BarMap>();
.SelectMany(foo => foo.Bars.Select(bar => new Bar { BarID = $"{foo.FooID}{bar.BarID}", BarProperty1 = bar.BarProperty1, BarProperty2 = bar.BarProperty2, BarProperty3 = bar.BarProperty3 }));
using (var reader = new StreamReader(memory))
content = reader.ReadToEnd();
Console.WriteLine(content);
public int FooID { get; set; }
public List<Bar> Bars { get; set; }
public string BarID { get; set; }
public string BarProperty1 { get; set; }
public string BarProperty2 { get; set; }
public string BarProperty3 { get; set; }
public class BarMap : ClassMap<Bar>
Map(bar => bar.BarID).Name("Id");
Map(bar => bar.BarProperty1).Name("Prop1");
Map(bar => bar.BarProperty2).Name("Prop2");
Map(bar => bar.BarProperty3).Name("Prop3");