CsvHelper ignore not working

You can use this label : [Ignore]

For example:

//Ignored value
[Ignore]
double value0 = 0;

//Serializable value
[Name("value 1")]
double value1 = 0;

The class map must be registered at runtime in order for CsvHelper to know to use it:

using (var csvWriter = new CsvWriter(textWriter))
{
    csvWriter.Configuration.RegisterClassMap<PersonClassMap>();
    csvWriter.WriteRecords(persons);
    textWriter.Flush();
}

Also note that, in the current version, you don't need to explicitly ignore fields in the class map (although this will change in the future):

Ignore

Currently this is not used. Mapping will only map properties that you specify. In the future there will be an option to auto map within a class map, and any mappings explicitly stated will override the auto mapped ones. When this happens, ignore will be used to ignore a property that was auto mapped.

With that in mind, you could also simplify your class map like so:

public sealed class PersonClassMap : CsvClassMap<Person>
{
    public PersonClassMap()
    {
        Map(m => m.Id).Index(0).Name("Id");
        Map(m => m.FirstName).Index(1).Name("First Name");
        Map(m => m.LastName).Index(2).Name("Last Name");
    }
}

Tags:

C#

.Net

Csvhelper