SQL 查询和系列/类型问题
Issue with SQL queries and Series / Type
这是我的代码,用于从我的 SQL 数据库请求 DateTime*float
(我使用:打开FSharp.Data.TypeProviders)
type FxTS = Series<DateTime,float>
let get (req: RFxTS) =
query { for d in db.Data_FXBFIX do
where (d.Fxpair = (req.FxPair.ToString())
&& (d.DatetimeUTC.Date >= req.Period.startDate)
&& (d.DatetimeUTC.Date <= req.Period.endDate))
sortBy d.DatetimeUTC
select (d.DatetimeUTC, d.Value) }
|> Series.ofObservations
|> FxTS
我在 FxTS 下遇到错误:(当我删除 FxTS 时,我可以看到这个函数 returns 确实是一个系列
Type constraint mismatch. The type
'Series<DateTime,float>'
is not compatible with type
'seq<Collections.Generic.KeyValuePair<DateTime,float>>'
谢谢
Series.ofObservation
适用于元组序列,不适用于 KeyValuePair。在 运行 Series.ofObservations 之前将其转换为元组即可。请参阅下面的示例。
open System
open System.Collections.Generic
open Deedle
let pairs =
[
KeyValuePair(DateTime(2018,1,31),1.)
KeyValuePair(DateTime(2018,2,28),2.)
]
pairs
|> Seq.map(fun x -> x.Key, x.Value)
|> Series.ofObservations
我想对@zyzhu 的回答补充的是
|> FxTS
是多余的。基本上,您可以摆脱显式类型注释
let get (req: RFxTS) : FxTS =
query { for d in db.Data_FXBFIX do
where (d.Fxpair = (req.FxPair.ToString())
&& (d.DatetimeUTC.Date >= req.Period.startDate)
&& (d.DatetimeUTC.Date <= req.Period.endDate))
sortBy d.DatetimeUTC
select (d.DatetimeUTC, d.Value) }
|> Series.ofObservations
只要 FxTS
只是 Series<DateTime,float>
的类型别名,编译器就希望它具有与 Series
相同的构造函数,其中之一确实是 seq<Collections.Generic.KeyValuePair<DateTime,float>>
这是我的代码,用于从我的 SQL 数据库请求 DateTime*float (我使用:打开FSharp.Data.TypeProviders)
type FxTS = Series<DateTime,float>
let get (req: RFxTS) =
query { for d in db.Data_FXBFIX do
where (d.Fxpair = (req.FxPair.ToString())
&& (d.DatetimeUTC.Date >= req.Period.startDate)
&& (d.DatetimeUTC.Date <= req.Period.endDate))
sortBy d.DatetimeUTC
select (d.DatetimeUTC, d.Value) }
|> Series.ofObservations
|> FxTS
我在 FxTS 下遇到错误:(当我删除 FxTS 时,我可以看到这个函数 returns 确实是一个系列
Type constraint mismatch. The type
'Series<DateTime,float>'
is not compatible with type
'seq<Collections.Generic.KeyValuePair<DateTime,float>>'
谢谢
Series.ofObservation
适用于元组序列,不适用于 KeyValuePair。在 运行 Series.ofObservations 之前将其转换为元组即可。请参阅下面的示例。
open System
open System.Collections.Generic
open Deedle
let pairs =
[
KeyValuePair(DateTime(2018,1,31),1.)
KeyValuePair(DateTime(2018,2,28),2.)
]
pairs
|> Seq.map(fun x -> x.Key, x.Value)
|> Series.ofObservations
我想对@zyzhu 的回答补充的是
|> FxTS
是多余的。基本上,您可以摆脱显式类型注释
let get (req: RFxTS) : FxTS =
query { for d in db.Data_FXBFIX do
where (d.Fxpair = (req.FxPair.ToString())
&& (d.DatetimeUTC.Date >= req.Period.startDate)
&& (d.DatetimeUTC.Date <= req.Period.endDate))
sortBy d.DatetimeUTC
select (d.DatetimeUTC, d.Value) }
|> Series.ofObservations
只要 FxTS
只是 Series<DateTime,float>
的类型别名,编译器就希望它具有与 Series
相同的构造函数,其中之一确实是 seq<Collections.Generic.KeyValuePair<DateTime,float>>