5
votes

So let's say I have a CSV file with a header containing columns Population and Profit, and I'd like to work with it in F# interactive. I have the following code:

#r "../packages/FSharp.Data.1.1.10/lib/net40/FSharp.Data.dll"

open FSharp.Data

// load csv header
let cities = new CsvProvider<"cities.csv">()

// how to reach data
let firstRow = cities.Data |> Seq.head
let firstPopulation = firstRow.Population
let firstProfit = firstRow.Profit

I get an error from F# interactive:

error FS0039: The field, constructor or member 'Population' is not defined

This seems confusing to me, because intellisense in VS has no problem picking up this column from my data via a CSV type provider.

Also, I tried creating a program with the same type provider and it all works just fine. Like this:

open FSharp.Data

[<EntryPoint>]
let main argv = 
    use file = System.IO.File.CreateText("result.txt")
    let csv = new CsvProvider<"cities.csv">()
    for record in csv.Data do
        fprintfn file "%A" record.Population
    0

Am I missing something? Thanks for any answer.

1
Can you try using full path to the CSV file? (Sometimes F# Interactive gets confused about paths...) - Tomas Petricek
That's weird. Are you sure you've sent all the lines to fsi? I just tried something similar an it worked. What version of VS are you using? - Gustavo Guerra
If there was a problem with the path it would give an explicit error, unless by change there's a cities.csv somewhere else without that Population column - Gustavo Guerra
This scenario may take place if you first named your column say xyzzy, locked the TP to FSI session by an attempt to execute, then renamed column to Population and tried executing again within the same FSI session. Then instance of TP within VS (and Intellisense) would be aware of property name change, but FSI-bound one wouldn't. Try resetting FSI session and repeat attempt to run. - Gene Belitski
Tomas, I have changed path to full path and it starts work. Also Gene you are right It was different column name in the first place. - Martin Bodocky

1 Answers

0
votes

Try this code

let Cities = new CsvProvider<"cities.csv">()
let cities = new Cities()
let firstRow = cities.Rows |> Seq.head