4

Extreme noob learning question ahead: I have a module that I'd like to use in my C# (universal) application. The C# app will download and unzip a file containing 12 CSVs that will always follow the same format. So what I've done is download the CSVs ahead of time, and added them to my solution so that the file referenced in CsvProvider<"thefile.csv"> will be there at compile time.

namespace ExperimentalFSLibrary    
module CsvHelper =
    open FSharp.Data
    let GetCsvA path = 
        CsvProvider<"thefileA.csv">.Load(path)

Then call this from my c# application like so:

var ReceivedCsvA = ExperimentalFSLibrary.CsvHelper.GetCsvA

I haven't had any success getting the data from the F# library

There's also the issue of how to deal with twelve different files, since I have to specify the template file for the CsvProvider, I imagine I'd have to write 12 different functions?

I've searched around SO and have found things that have gotten me this far but I've hit a wall. Any help including a sanity check would be very appreciated.

1 Answer 1

5

The CsvProvider in FSharp.Data is an erasing type provider. This means there is no type to consume and use from within C#.

Many F# type providers, such as this, will only be beneficial when used from within F#.

In this case, you'd most likely be better off parsing the CSV data and returning the results you need via some API consumable from your C# code.

There's also the issue of how to deal with twelve different files, since I have to specify the template file for the CsvProvider, I imagine I'd have to write 12 different functions?

In general, you'd need a separate function per file structure, not per file. If the 12 files all use the same columns/headers/etc, then a single type provider type will work across each of them. The static filename provided is used only to determine the column structure of the generated types.

2
  • Thank you for the response! - Would doing more of my business logic within the F# and returning something more basic be beneficial? Maybe a type defined in C# and loading it with the F# code - I would use C# but there isn't a library that works well with 8.1 universal apps that I've liked enough. Or a good, native/built in c# parser Jun 18, 2014 at 2:55
  • 1
    @t0rqued That's exactly what I would do - my suggestion would be to keep as much of the logic in F# as possible - it's so much cleaner for handling parsing and transformations... Just pass back objects to C# at the end. Jun 18, 2014 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.