Skip to content

ClassMap handle the value of an optional column - null or empty value #1871

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

You must be logged in to vote
void Main()
{
    var s = new StringBuilder();
    s.Append("Id,Name\r\n");
    s.Append("1,\r\n");
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
    };
    using (var reader = new StringReader(s.ToString()))
    using (var csv = new CsvReader(reader, config))
    {
        csv.Configuration.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).Optional();
    }
}

void Main()
{
    var s = new StringBuilder();
    s.Append("Id\r\n"

Replies: 4 comments 6 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

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

Answer selected by vernetk
Comment options

You must be logged in to vote
5 replies
@JoshClose
Comment options

@avdimdv
Comment options

@JoshClose
Comment options

@avdimdv
Comment options

@JoshClose
Comment options

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