3

是否可以在 F# 中使用 lambda 样式查询 IQueryable 对象,而不是查询表达式?就像是:

type schema = SqlDataConnection<"Data Source=(local);Initial Catalog=MyDatabase;Integrated Security=true;">
let db = schema.GetDataContext()
let q = db.MyTable |> Seq.filter (fun r -> r.id < 100) |> Seq.take 10
let result = q |> List.ofSeq

当我对此进行分析时,它正在这样做select * from MyTable,所以我假设过滤器和拍摄不是在执行IEnumerablesIQueryables

query {}或者是在没有 lambda的情况下解决此问题的唯一方法?

4

1 回答 1

0

原因是Seq.toList调用数据结构 GetEnumerator() 并且类型内部有这样的东西(伪,而不是实际源):

type SqlDataConnection<...>(...) =
    interface seq<'T> with
        member __.GetEnumerator() = executeSQL()

如果您想使用 IQueryable 而不是 IEnumerable 进行操作,则 F# 中有查询语法:

query {
    for r in db.MyTable do
    where (r.id < 100)
    take 10
} |> Seq.toList

更多详细信息: https ://docs.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/query-expressions

于 2016-08-31T15:23:04.950 回答