f#将seq obj转换为seq记录

时间:2019-04-26 21:15:06

标签: f#

我正在尝试从数据库读取数据并将其转换为记录的序列。 执行

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',但是这里有类型       ”一个选项”

我做错了什么?

1 个答案:

答案 0 :(得分:1)

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

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

...使用看起来像这样的图案...

| Some itemName, Some itemAuthor ->

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

| Some itemName, itemAuthor ->