3

无论出于何种原因,我都不能在查询表达式中使用“for”构造。我得到的是以下错误。我刚刚选择的代码曾经可以工作->“错误:只有在计算表达式构建器定义了'For'方法时才能使用此控件构造”

#r "System.Data.Linq.dll"
#r "FSharp.Data.TypeProviders.dll"

open FSharp.Linq
open FSharp.Data.TypeProviders

[<Literal>]
let connectionString' = @"
    data source = ...;
    initial catalog = NORTHWND;
    Integrated Security = SSPI"

type NorthwndDb =
    SqlDataConnection<connectionString', Pluralize = true>

let db' = NorthwndDb.GetDataContext()

let curstomerSortedByCountry =
    query { for c in db'.Customers do
                sortBy c.Country
                select (c.Country, c.CompanyName) } 
    |> Seq.cache
4

1 回答 1

8

我无法使用您的代码片段对此进行测试(没有数据库可以对其进行测试),但我认为如果您在表达式query之前在代码片段中的某处定义了一个名为的变量,则会发生您报告的错误query. 例如:

let query = "oops!"
let curstomerSortedByCountry =
    query { for c in [1 .. 10] do
            sortBy c
            select c } 

错误 FS0708:仅当计算表达式构建器定义了“For”方法时,才可以使用此控制构造

原因是queryin中的标识符query { .. }只是标准 F# 库中声明的变量,它具有各种成员,包括query.For. 如果您用自己的声明隐藏它,则将找不到该成员。

于 2017-10-08T19:09:33.287 回答