Reputation: 402
I have a C# console application that calls a F# library that does some CSV parsing by using the CSVTypeProvider. (http://fsharp.github.io/FSharp.Data/reference/fsharp-data-csvprovider.html)
Unfortunately i'm somewhat new to F# and so i haven't found a way to efficiently hand the parsed data from F# to C# in form of a List of C# objects.
Suppose i have an C# Data Model:
public class Customer{
public int ID { get; set; }
public string Name { get; set; }
}
Then i want to transform the data from the csv type provider to a list of that model:
Extractor extractor = new Extractor();
List<Customer> customers = extractor.Data;
With the Extractor being 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 simply import the Datamodel into the f# library and use the map function to map the row 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 simply have to create the class that 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 rows to the customer objects and import the customer type to c#.
Upvotes: 3
Views: 334
Reputation: 2291
I would define my model class in the F# project like so
type Customer = {id:int;name:string}
then in your map you can
Seq.map(fun row -> {id=row.id;name=row.name})
and return as an IEnumerable to your C# code.
Upvotes: 3