指定架构时是否可以使用带有 FSharp.Data.CsvProvider 的自定义类型?

Is it possible to use a custom type with the FSharp.Data.CsvProvider when specifying a Schema?

我在 F# 中使用如下所示的 CSV:

When,Where,HowMuch
11/24/2019,Germany,100 EUR
11/25/2019,France,100 EUR
11/26/2019,Switzerland,50 CHF
11/27/2019,USA,75 USD

我正在使用 CSV Type Provider in the FSharp.Data 包来解析此数据。

type CurrencyDetector = CsvProvider<"Currencies.csv">

显然第一列是日期,第二列是字符串。

对于第三个,我喜欢使用这种类型:

type Money (amountAndDenomination : string) =

    let parts = amountAndDenomination.Split ' '

    member __.Amount = Decimal.Parse parts.[0]
    member __.Denomination = parts.[1]

我在我的 CsvProvider 行中尝试了 Schema 参数的一些排列,但到目前为止没有任何效果。例如:

type CurrencyDetector = CsvProvider<"Currencies.csv",Schema="When (date),Where (string),HowMuch (Money)">

When 输出为 DateTimeWhere 输出为 string,但 HowMuch 变成字符串 属性,名称为 HowMuch (Money):

有没有办法将我自己的 类 与 CsvProvider 一起使用,或者这是不可能的?

根据 documentation for the CsvProvider,我认为这不可能:

Schema 参数:“可选列类型,以逗号分隔列表。有效类型是 int、int64、bool、float、decimal、date、guid、 string, int?, int64?, bool?, float?, decimal?, date?, guid?, int 选项, int64 选项, bool 选项, float 选项, decimal 选项, date 选项, guid 选项和 string 选项。您还可以指定单位 和列名,如下所示:名称 (type<\unit>),或者您可以只覆盖名称。如果您不想指定所有列,您可以按名称引用列,如下所示:ColumnName=type."

但是请注意,在上面,使用units of measure的可能性。因此,您可能会探索为货币面额创建计量单位。不过,类型提供程序 可能 需要该列的不同格式。

documentation for the CSV Type Provider, you can find further information about units of measure and also how to "transform the columns themselves by using Map", which should enable you to map the string type to a custom type. Looking at the source code中,表明Rows集合是一个Row对象的序列,每个对象都是一个元组。因此,您应该能够使用 Seq.map and/or Seq 模块中的任何其他函数来 post 处理生成的集合。