Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
Convert or filter members in CsvHelper when writing

Imagine I have a class like this

public class DataGroup 
{
    public string DataA { get; set; }
    public string DataB { get; set; }
    public bool Updated { get; set; }
}

I need to write an empty string or "N/A" for DataA and DataB when Updated == false, and the actual value when Updated == true;

Is this possible with CsvHelper?

Type conversion does not seem to work as it only access the final member so it is not able to read the value of Updated.

There is a Conversion method for the MapMember class but it seems to only work for reading.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use the Convert method. This method can be a little confusing because it can either ConvertFromString or ConvertToString, depending on how you use it.

ConvertFromString Use Row with CsvReader

Map(m => m.DataA).Convert(m => m.Row.GetField("DataA") + m.Row.GetField("Updated"));

ConvertToString Use Value with CsvWriter

Map(m => m.DataA).Convert(m => m.Value.DataA + m.Value.Updated);

You want ConvertToString, so your ClassMap could be something like this.

public class DataGroupMap : ClassMap<DataGroup>
{
    public DataGroupMap()
    {
        Map(m => m.DataA).Convert(m =>
        {
            if (m.Value.Updated) { return m.Value.DataA; }
            return "N/A";
        });
        Map(m => m.DataB).Convert(m =>
        {
            if (m.Value.Updated) { return m.Value.DataB; }
            return "N/A";
        });
        Map(m => m.Updated);
    }
}
over 4 years ago · Santiago Trujillo Report

0

if(Updated == false)
{
    DataA = "N/A";
    DataB = "N/A";
}
else
{
     DataA = "Actual Information";
     DataB = "Actual Information";
}

Instead of "N/A" you can set the type to null

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!