簡體   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