The CSV type provider from FSharp.Data is primarily known and used for reading CSVs (as the name suggests), but it's also quite capable of writing CSVs as well.
All you need to do is to define the type, either by providing a sample .CSV file
let titanic2 = CsvProvider<"../data/Titanic.csv", Schema="Fare=float,PClass->Passenger Class">.GetSample()
or by directly defining the schema
type MyCsvType = CsvProvider<Schema = "A (int), B (string), C (date option)", HasHeaders=false>
then you can create a record object and populate it (in a type-safe way!)
// you can build the rows themselves
let myCsv = new MyCsvType( [ MyCsvType.Row(1, "a", None)
MyCsvType.Row(2, "B", Some DateTime.Now) ])
// or, for your scenario, you probably want to define a conversion function
// from your record type to the CSV provider's type
let buildRowFromObject obj = MyCsvType.Row(obj.A, obj.B, obj.C)
let buildTableFromObjects = (Seq.map buildRowFromObject) >> Seq.toList >> MyCsvType
let myCsv = someSequenceOfObjects |> buildTableFromObjects
and finally, just call
myCsv.SaveToString()
to get the output in CSV format.