繁体   English   中英

f#将seq obj转换为seq记录

[英]f# convert seq obj to seq records

我试图从数据库中读取数据,并将其转换为记录的序列。 执行时

open FSharp.Data
open FSharp.Data.SqlClient


type foo = {name:int; author:int}

[<Literal>]
let query = "Select * from Authors"

[<Literal>]//DESKTOP-5RIV0K1
let connectionString = "Data Source=DESKTOP-5RIV0K1;Initial Catalog=TestBase;Integrated Security=True"

let cmd = new SqlCommandProvider<query,connectionString>(connectionString)

let result = cmd.Execute() |> printfn "%A"

result

我懂了

seq
  [{ AuthorID = 1; Name = Some "2" }; { AuthorID = 2; Name = Some "3" };
   { AuthorID = 3; Name = Some "4" }] 

但是当我尝试使用以下代码将seq obj转换为seq foo时

let result = 
    for item in cmd.Execute() do
    match item.Name, item.AuthorID with
        | Some itemName, Some itemAuthor ->
            {name = itemName; author = Some itemAuthor }
        | _ -> ()

我得到错误

一些itemAuthor

错误FS0001该表达式应为'int'类型,但此处为'a option'类型

我做错了什么?

您正在尝试匹配如下所示的记录...

{ AuthorID = 1; Name = Some "2" }

...使用看起来像这样的模式...

| Some itemName, Some itemAuthor ->

请注意,您的AuthorID是整数。 它不是Option<int> 因此,具有两个Some值的模式不兼容。 您应该改用这样的模式...

| Some itemName, itemAuthor ->

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM