如何在我的CsvClassMap中为我的类中不存在的属性添加列?
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")
}
}
记录ID,ID,名称
1Bob,1,鲍勃
答案 0 :(得分:0)
写入CSV时,我更喜欢手动将对象映射到与我期望的列完全匹配的类。它非常灵活,明确,并且不需要编写CSV类映射所需的代码。在你的情况下,它看起来像这样
var customerRows = customers.Select(c => new {RecID = c.ID + c.Name, c.ID, c.Name });
然后你会将customerRows写入CSV。