Search code examples
typesf#type-providers

What is the type of a row in a Csv type provider?


I created a Csv type provider:

type AssetInfo = CsvProvider<Sample="AssetInfoFS.csv", // must be a Literal
                             HasHeaders=true,
                             ResolutionFolder=projDataPath,
                             AssumeMissingValues=false,
                             CacheRows=false>

Then I loaded some info and discarded some rows:

let assetInfo = AssetInfo.Load(projDataPath + @"AssetInfoFS.csv")
let traderInfoRecords =
    assetInfo.Rows
    |> Seq.filter (fun elem -> Set.contains elem.Ticker tradedTickerSet)

Next I tried to create a record type that would have a row as one of its values:

type Asset = {Ticker: string; Info: CsvProvider<...>.Row; DataFrame: Frame<DateTime,string>}

I got CsvProvider<...>.Row by using MoveNext() and then Current and GetType() on an IEnumerator for assetInfo.Rows. Unfortunately the compiler does not accept that expression as a type. I tried other things with no success.

How can I find the type of a row of assetInfo so that I can use it in the Asset type declaration?


Solution

  • In your case, the row will have the type AssetInfo.Row.

    I made a brief example where I have a CSV with two columns, named Header1 and Header2. I can now do the following:

    module Test
    
    open FSharp.Data
    
    type File = CsvProvider<"""..\test.csv""">
    
    let f (input : File.Row) = input.Header1
    

    Where by explicitly naming the type of the argument to f, Intellisense shows me both of the fields on the type.