简体   繁体   中英

F# Data CsvProvider use from a C# application Interop

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM