简体   繁体   中英

Adding new member to CsvProvider type in F#

I'm reading a CSV file in F# with CsvProvider from FSharp.Data. Is there a way to add new (calculated) columns without transforming it into an entirely new type?

open FSharp.Data

type Person = CsvProvider<"persons.csv">
let personData = Person.Load "persons.csv"

If Person has a member called YearOfBirth then is it possible to add a new member to it called Age that would have the calculated value of (DateTime.Now.Year - YearOfBirth) for all CSV rows?

It depends on what you want, but this may work:

type Person.Row with
    member this.Age = DateTime.Now.Year - this.YearOfBirth

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