Search code examples
c#.net-6.0csvhelpercsvreader

how do you combine columns into one field with CSVhelper


How do you combine columns into one field with CsvHelper?

I'm using CsvHelperby joshclose. Is possible to join two columns into one when doing the mapping?

I.e csv file has two headers, first and last name. I want to combine then into one field call fullname when doing the mapping. can do them individually.

Map(m => m.ContactFullName).Name("FIRST");
Map(m => m.ContactFullName).Name("LAST");

Solution

  • You can try something like this in your class that inherits ClassMap.

    Map(m => m.ContactFullName).ConvertUsing(row => row.GetField("FIRST") + " " + row.GetField("LAST"));
    

    Another approach is the DefaultTypeConverter: https://joshclose.github.io/CsvHelper/examples/configuration/class-maps/type-conversion/