JoshClose / CsvHelper

Library to help reading and writing CSV files
http://joshclose.github.io/CsvHelper/
Other
4.73k stars 1.06k forks source link

Add ability to read empty string as 0 (or other default value) for non-nullable types #2217

Open DanielStout5 opened 9 months ago

DanielStout5 commented 9 months ago

Is your feature request related to a problem? Please describe. Entities in e.g. EF Core often have a non-nullable ID. I'd like to be able to import data for them from a CSV file where the ID will sometimes be specified and sometimes not.

It would make sense to be able to omit the ID from the CSV in the case we aren't specifying it - so effectively it would be treated as 0.

Describe the solution you'd like We are already able to configure values that are interpreted as true/false boolean values, e.g. csv.Context.TypeConverterOptionsCache.GetOptions<bool>().BooleanTrueValues.AddRange(new[] { "Y", "Yes" });

It seems like more generally there could be a way to map equivalent values - that's effectively what the above is doing: "Y" and "Yes" map to (bool) true. I'd like to also specify that "" or null map to (int) 0.

Describe alternatives you've considered Could add a separate class just for use with CSVHelper and make the property nullable.