Skip to content

Is there a way to read null records? #1795

Answered by JoshClose
fquinsol asked this question in Q&A
Discussion options

You must be logged in to vote

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(

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@HunterAhlquist
Comment options

Answer selected by JoshClose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants