如何将给定类型的对象转换为记录?
How can I transform an object of a given type to a record?
我通过使用 SqlCommandProvider
获得了一个 IEnumerable 对象。这些对象匹配我的行和列。让我们称之为 B。在我的程序中,我也创建了一个匹配这些列的类型。让我们将此记录称为 A。我实际上如何 transform/cast/construct 基于 B 的值的类型 A 的记录 c?
type A = {k: int; c: string}
let a:A = { k = 1; c = "c" }
let b = {| k = 1; c = "c" |} // lets say that this one simulate the sql's object
let c:A = b
printfn "%A" a
printfn "%A" b
printfn "%A" c
我收到一个类型不匹配的错误:
error FS0001: This expression was expected to have type 'A' but here has type '{|c : string ; k : int|}'
提供者的完整示例代码:
type A = {k: int; c: string}
let toA c =
(A)c // ???
do
use cmd = new SqlCommandProvider<"
SELECT k, c
FROM Examples
" , connectionString>(connectionString)
cmd.Execute() |> Seq.map toA
sql table 如果重要的话:
k
c
0
a
1
b
我阅读了 doc 但没有找到解决问题的方法。可能是理解问题。
一个有效的解决方案是从对象构造一个记录:
let toA c =
{k = c.k; c = c.c }
cmd.Execute()
|> Seq.map toA
我通过使用 SqlCommandProvider
获得了一个 IEnumerable 对象。这些对象匹配我的行和列。让我们称之为 B。在我的程序中,我也创建了一个匹配这些列的类型。让我们将此记录称为 A。我实际上如何 transform/cast/construct 基于 B 的值的类型 A 的记录 c?
type A = {k: int; c: string}
let a:A = { k = 1; c = "c" }
let b = {| k = 1; c = "c" |} // lets say that this one simulate the sql's object
let c:A = b
printfn "%A" a
printfn "%A" b
printfn "%A" c
我收到一个类型不匹配的错误:
error FS0001: This expression was expected to have type 'A' but here has type '{|c : string ; k : int|}'
提供者的完整示例代码:
type A = {k: int; c: string}
let toA c =
(A)c // ???
do
use cmd = new SqlCommandProvider<"
SELECT k, c
FROM Examples
" , connectionString>(connectionString)
cmd.Execute() |> Seq.map toA
sql table 如果重要的话:
k | c |
---|---|
0 | a |
1 | b |
我阅读了 doc 但没有找到解决问题的方法。可能是理解问题。
一个有效的解决方案是从对象构造一个记录:
let toA c =
{k = c.k; c = c.c }
cmd.Execute()
|> Seq.map toA