我正在尝试将ODataService类型提供程序用于Netflix。这工作正常:
type internal NetflixData = ODataService<"http://odata.netflix.com/Catalog/">
let internal NetflixContext = NetflixData.GetDataContext()
let godzillamovies = query { for t in NetflixContext.Titles do
where (t.Name.Contains "Godzilla")
select (t.Name, t.ReleaseYear)
} |> Seq.toList
但返回哥斯拉电视节目的所有剧集,没有发布年份日期(boo)。因此,我将查询更新为:
let godzillamovies = query { for t in NetflixContext.Titles do
where (t.Name.Contains "Godzilla" && t.ReleaseYear.HasValue)
select (t.Name, t.ReleaseYear.Value)
} |> Seq.toList
而且我遇到了以下错误:
<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>
<error xmlns=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\">
<code></code>
<message xml:lang=\"en-US\">No property 'HasValue' exists in type 'System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' at position 45.</message>
</error>
呃,可为空的int不存在HasValue吗?从何时起?
最佳答案
#r "FSharp.Data.TypeProviders"
#r "System.Data.Services.Client"
open Microsoft.FSharp.Data.TypeProviders
open Microsoft.FSharp.Linq.NullableOperators
type internal NetflixData = ODataService<"http://odata.netflix.com/Catalog/">
let internal NetflixContext = NetflixData.GetDataContext()
NetflixContext.DataContext.SendingRequest.Add(fun e -> printfn "%A" e.Request.RequestUri)
// http://odata.netflix.com/Catalog/Titles()?$filter=substringof('Godzilla',Name) and (ReleaseYear ne null)&$select=Name,ReleaseYear
let godzillamovies = query { for t in NetflixContext.Titles do
where (t.Name.Contains "Godzilla")
where (t.ReleaseYear ?<>? System.Nullable())
select (t.Name, t.ReleaseYear)
} |> Seq.toList