【问题标题】:F# groupBy - System.Exception : unrecognized method callF# groupBy - System.Exception:无法识别的方法调用
【发布时间】:2016-04-26 12:17:01
【问题描述】:

我正在尝试使用 F# SqlDataProvider 查询数据,但是当我想使用 groupBy 函数时出现奇怪的错误

我的初始化代码:

r# "packages/FSharp.Data.2.2.5/lib/net40/FSharp.Data.dll"
r# "packages/SQLProvider.1.0.0/lib/FSharp.Data.SQLProvider.dll"
r# "packages/FSharp.Data.TypeProviders.5.0.0.2/lib/net40/FSharp.Data.TypeProviders.dll"

open FSharp.Data
open FSharp.Data.Sql
open FSharp.Data.TypeProviders
open FSharp.Linq
open System.Text.RegularExpressions
open System
open System.Data

type dbSchema = SqlDataProvider<
                    ConnectionString = "my-connection-string",
                    DatabaseVendor = Common.DatabaseProviderTypes.MSSQLSERVER,
                    IndividualsAmount = 1000,
                    UseOptionTypes = true>
let db = dbSchema.GetDataContext()

我的查询:

query {
    for county in db.Dbo.Countries do
    groupBy county.CountryCode into g
    select (g.Key, g.Count())
    } |> Seq.iter (fun (key, count) -> printfn "%s %d" key count)

我收到了这个错误:

System.Exception:无法识别的方法调用 在 Microsoft.FSharp.Linq.RuntimeHelpers.LeafExpressionConverter.EvaluateQuotation(FSharpExpr e) 在 Microsoft.FSharp.Linq.QueryModule.EvalNonNestedInner(CanEliminate canElim,FSharpExpr queryProducingSequence) 在 Microsoft.FSharp.Linq.QueryModule.clo@1735-1.Microsoft-FSharp-Linq-ForwardDeclarations-IQueryMethods-Executea,b 在 C:\Development\CountriesParser\Script1.fsx: 36 行中的 .$FSI_0003.main@() 处

第 36 行是 groupBy 的确切行。

正如我在这些页面中所读到的,它应该可以工作 http://fsprojects.github.io/FSharp.Linq.ComposableQuery/QueryExamples.html https://msdn.microsoft.com/en-us/library/hh225374.aspx

【问题讨论】:

  • 我在这里没有立即看到任何错误 - 您能否包括您引用的库以及打开的命名空间? db 的价值是如何创造出来的?
  • 我编辑了我的问题并在查询前添加了代码,谢谢

标签: linq f#


【解决方案1】:

F# 有许多不同的 SQL 类型提供程序,我认为您的代码使用社区驱动的提供程序,目前不支持 groupBy 构造。

  1. SqlDataProvider 类型来自SQLProvider 包并记录在here
  2. FSharp.Data.TypeProviders 库(也与 Visual Studio 一起分发)记录在 here 并公开了 SqlDataConnection 类型。
  3. 您还提到了记录在 here 的 ComposableQuery 库,这是您可以在 (2) 之上使用的扩展,但您没有在代码中这样做。

库 (1) 在很多方面都更好,但还不支持完整的查询语言,因此您可能需要切换到 (2)。

为此,只需引用FSharp.Data.TypeProviders(您不需要其他两个包),然后在MSDN Walkthrough on the topic 之后使用SqlDataConnection 类型

【讨论】:

    猜你喜欢
    • 2021-02-11
    • 1970-01-01
    • 2012-01-01
    • 2020-05-16
    • 2012-12-11
    • 2021-11-21
    • 2016-04-19
    • 2017-07-24
    • 1970-01-01
    相关资源
    最近更新 更多