Search code examples
c#linqcsvdecimal-pointlinqtocsv

LinqToCSV and decimal globalization


I'm trying to read a csv file using LingToCSV, containing decimal values with a comma a seperator instead of a point.

This is one row of input:

"DataField1","DataField2"
"Test", "0,34"

And I use this class to read it:

public class Record
{
   [CsvColumn(Name = "DataField1", FieldIndex=  1)]
   string DataField1 {get;set;}

   [CsvColumn(Name = "DataField2", FieldIndex=  2, OutputFormat="C", NumberStyle=NumberStyles.Currency)]
   decimal DataField2 {get;set;}
}

However, this doesn't work and generates this exception:

{"Value \"DataField2\" in line 9 has the wrong format for field or property \"DataFoe;d2\" in type \"Record\"."}

Obviously it's expecting a point, but how can I make it work using the commas.


Solution

  • Believe you need to set the FileCultureName in the CsvFileDescription...

    CsvFileDescription fileDesc = new CsvFileDescription
            {
    
                FirstLineHasColumnNames = true
                FileCultureName = "nl" 
            };