-
CSVHelper if perfectly happy writing null records as - ,,,,,, - Thanks |
Beta Was this translation helpful? Give feedback.
Answered by
JoshClose
May 6, 2021
Replies: 1 comment 1 reply
-
If the type is nullable, this works without any config. If the type is a string, it'll be a string and you need to set an empty string as a null value. void Main()
{
var s = new StringBuilder();
s.Append("Id,Name\r\n");
s.Append(",\r\n");
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
};
using (var reader = new StringReader(s.ToString()))
using (var csv = new CsvReader(reader, config))
{
csv.Context.RegisterClassMap<FooMap>();
csv.GetRecords<Foo>().ToList().Dump();
}
}
private class Foo
{
public int? Id { get; set; }
public string Name { get; set; }
}
private class FooMap : ClassMap<Foo>
{
public FooMap()
{
Map(m => m.Id);
Map(m => m.Name).TypeConverterOption.NullValues(string.Empty);
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
JoshClose
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If the type is nullable, this works without any config. If the type is a string, it'll be a string and you need to set an empty string as a null value.