F # CSV Parsing for C # Application
I have a C # console application that calls an F # library that does some CSV parsing using a CSVTypeProvider. ( http://fsharp.github.io/FSharp.Data/reference/fsharp-data-csvprovider.html )
Unfortunately, I'm somewhat new to F #, so I haven't found a way to efficiently pass parsed data from F # to C # as a list of C # objects.
Suppose I have a C # data model:
public class Customer{
public int ID { get; set; }
public string Name { get; set; }
}
Then I want to convert data from a csv type provider to a list of this model:
Extractor extractor = new Extractor();
List<Customer> customers = extractor.Data;
When Extractor is defined as:
module internal FileLoader = type = Customers = CsvProvider<"C:\somefilePath", HasHeaders = true> let customers = Customers.Load<"C:\someFilePath"> type Extractor() = member this.Data = FileLoader.customers.Rows |> Seq.map(fun row -> new Customer(row.ID, row.Name))
From there, I thought I could just import the Datamodel into the f # library and use the map function to map the string values to the C # object, but that doesn't quite seem to work.
Edit:
I found a solution, but I'm still open to a more elegant one.
I just need to create the class I want in C # (Customer) in the F # library.
type Customer(ID : int, Name : string) = member this.ID = ID member this.Name = Name
Then I can use the mapping function to convert the strings to client objects and import the client type in C #.
source to share