How do I add a column to my CsvClassMap for a property that doesn't exist in my class?
public class Customer {
public int ID { get; set; }
public string Name { get; set; }
}
public sealed class CustomerMap : CsvClassMap<Customer>
{
public CustomerMap() {
Map(r => r.ID).Index(1)
Map(r => r.Name).Index(2)
//need a column called Rec ID that is simple concatenation of ID and Name
Map(r => ???).Index(0).Name("Rec ID")
}
}
Rec ID,ID,Name
1Bob,1,Bob
When writing to CSV, I prefer to manually map objects to a class that perfectly matches my expected columns. It's very flexible, explicit, and no more code than needed to write a CSV class map. In your case it would look something like this
var customerRows = customers.Select(c => new {RecID = c.ID + c.Name, c.ID, c.Name });
Then you would write customerRows to the CSV.