I am trying to save an F# record collection to a csv file using the .Net CsvHelper library. The problem is that option types are not being converted to string correctly.
#r "nuget: CsvHelper"
open System.IO
open System.Globalization
open CsvHelper
type Record = { X : string; Y : float option }
let writeString x =
use writer = new StringWriter()
use csv = new CsvWriter(writer, CultureInfo.InvariantCulture)
csv.WriteRecords(x)
writer.ToString()
[{ X = "hi"; Y = Some 1.00}
{ X = "bye"; Y = None }]
|> writeString
I expect a blank value (or any other nan value that CsvProvider will understand) for the second field in the second data row here. Instead, CsvHelper is converting the None value to 0.
val it : string = "X,Value
hi,1
bye,0
"
I am aware that FSharp.Data.CsvProvider would let me convert this simple record to a CsvFile.Row that can be saved. But that's a poor solution when you have many fields because it is hard to keep the order of the fields in the CsvFile.Row straight (imagine 50 float fields). I'd prefer a solution in which the record fields are converted automatically.