将查询和函数传递给另一个函数
Pass query and function to another function
我有一个很好用的方法:
member this.GetItems =
let db = dbSchema.GetDataContext()
let getQuery =
query {
for row in db.ItemsTable do
select row
}
getQuery
|> Seq.map (fun e -> new Item(e.ItemId, e.ItemName ))
|> Seq.toArray
它对数据库进行 select 查询,将每条记录转换为对象和 returns 此类对象的数组。
我想把这个方法一分为二。第一个必须更笼统。它获取查询和将记录转换为对象的函数。
这是我的代码:
member private this.ExecuteSelectQuery(query, transform_function) =
let db = dbSchema.GetDataContext()
query
|> Seq.map transform_function
|> Seq.toArray
member this.GetItems =
let db = dbSchema.GetDataContext()
this.ExecuteSelectQuery
query {
for row in db.ItemsTable do
select row
}
(fun e -> new Item(e.ItemId, e.ItemName ))
但是我得到错误:
- 在此表达式中需要类型 'a * ('b -> 'c) 但有一个类型 QueryBuilder
- 此构造只能用于方法 For
我该如何解决?
更新:
member private this.ExecuteSelectQuery query transform_function =
let db = dbSchema.GetDataContext()
query
|> Seq.map transform_function
|> Seq.toArray
member this.GetItems =
let db = dbSchema.GetDataContext()
this.ExecuteSelectQuery
query {
for row in db.ItemsTable do
select row
}
(fun e -> new Item(e.ItemId, e.ItemName ))
成员函数ExecuteSelectQuery
需要参数作为元组,所以你必须使用括号。
member private this.ExecuteSelectQuery(query, transform_function) =
query
|> Seq.map transform_function
|> Seq.toArray
member this.GetItems =
use db = dbSchema.GetDataContext()
this.ExecuteSelectQuery (
query {
for row in db.ItemsTable do
select row
},
(fun e -> new Item(e.ItemId, e.ItemName )))
请同时考虑用于绑定 DataContext
实例的 use
关键字,以正确实现一次性模式。我已经在我的示例中完成了。
我有一个很好用的方法:
member this.GetItems =
let db = dbSchema.GetDataContext()
let getQuery =
query {
for row in db.ItemsTable do
select row
}
getQuery
|> Seq.map (fun e -> new Item(e.ItemId, e.ItemName ))
|> Seq.toArray
它对数据库进行 select 查询,将每条记录转换为对象和 returns 此类对象的数组。
我想把这个方法一分为二。第一个必须更笼统。它获取查询和将记录转换为对象的函数。
这是我的代码:
member private this.ExecuteSelectQuery(query, transform_function) =
let db = dbSchema.GetDataContext()
query
|> Seq.map transform_function
|> Seq.toArray
member this.GetItems =
let db = dbSchema.GetDataContext()
this.ExecuteSelectQuery
query {
for row in db.ItemsTable do
select row
}
(fun e -> new Item(e.ItemId, e.ItemName ))
但是我得到错误:
- 在此表达式中需要类型 'a * ('b -> 'c) 但有一个类型 QueryBuilder
- 此构造只能用于方法 For
我该如何解决?
更新:
member private this.ExecuteSelectQuery query transform_function =
let db = dbSchema.GetDataContext()
query
|> Seq.map transform_function
|> Seq.toArray
member this.GetItems =
let db = dbSchema.GetDataContext()
this.ExecuteSelectQuery
query {
for row in db.ItemsTable do
select row
}
(fun e -> new Item(e.ItemId, e.ItemName ))
成员函数ExecuteSelectQuery
需要参数作为元组,所以你必须使用括号。
member private this.ExecuteSelectQuery(query, transform_function) =
query
|> Seq.map transform_function
|> Seq.toArray
member this.GetItems =
use db = dbSchema.GetDataContext()
this.ExecuteSelectQuery (
query {
for row in db.ItemsTable do
select row
},
(fun e -> new Item(e.ItemId, e.ItemName )))
请同时考虑用于绑定 DataContext
实例的 use
关键字,以正确实现一次性模式。我已经在我的示例中完成了。