SQL查询和系列/类型问题

时间:2019-04-10 10:56:05

标签: f# deedle

这是我的代码,用于从我的SQL DB请求一个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时,我可以看到此函数确实返回了Series

Type constraint mismatch. The type 
    'Series<DateTime,float>'    
is not compatible with type
    'seq<Collections.Generic.KeyValuePair<DateTime,float>>' 

谢谢

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

我想补充到@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