Search code examples
c#csvhelper

How implement Boolean convertor When reading CSV file using CSVHelper libraries


My CSV file has values of YES, NO, True, and False (OR) empty. Do we have any option to convert bool from Yes, No, False, True, or empty in the latest version of CSVhelper libraries? (OR) need to implement any converter?

sample data:

ACCOUNTID,IsValid
1,yes
2,True
3,No
4,
5,false

Solution

  • Take a look at BooleanTrueValues and BooleanFalseValues attributes.

    Looks like you can just add the attributes to the IsValid property

    https://joshclose.github.io/CsvHelper/examples/configuration/attributes/

    e.g.:

    public class CsvData
    {
        public int AccountId{ get; set; }
    
        [BooleanTrueValues("yes", "True")]
        [BooleanFalseValues("no", "False")]
        public bool IsValid{ get; set; }
    
    }