Home > OS >  CsvHelper: modify a string before loading it into a ClassMap?
CsvHelper: modify a string before loading it into a ClassMap?

Time:10-29

Using CsvHelper v29, when creating a ClassMap, is it possible to modify a string before it is loaded into the map?

For example, in the code below, m.EmployeeNumber is of type string? and it may have a value of "001234":

    public sealed class CsvMap : ClassMap<Employee>
    {
        public CsvMap()
        {
            _ = Map(m => m.EmployeeNumber);
            ...
        }
    }

I'm trying to convert its value using Map() to "=001234".

I have tried: _ = Map(m => m.EmployeeNumber?.Prepend('='));

But this will not work because an expression tree lambda cannot have the null propagating operator.

CodePudding user response:

The bigger issue is that Map is expecting the expression to return a class member, for example EmployeeNumber. You would need to use Convert to actually change the output.

_ = Map(m => m.EmployeeNumber).Convert(args => "="   args.Value.EmployeeNumber);
  • Related