简体   繁体   English

f#将seq obj转换为seq记录

[英]f# convert seq obj to seq records

I am trying to read data from database and convert them to seq of records. 我试图从数据库中读取数据,并将其转换为记录的序列。 When executing 执行时

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

I get 我懂了

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

But when I am trying to convert seq obj to seq foo with code below 但是当我尝试使用以下代码将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 }
        | _ -> ()

I get error 我得到错误

Some itemAuthor 一些itemAuthor

Error FS0001 This expression was expected to have type 'int' but here has type ''a option' 错误FS0001该表达式应为'int'类型,但此处为'a option'类型

What I am doing wrong? 我做错了什么?

You are trying to match records that look like this... 您正在尝试匹配如下所示的记录...

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

...using a pattern that looks like this... ...使用看起来像这样的模式...

| Some itemName, Some itemAuthor ->

Notice that your AuthorID is an integer. 请注意,您的AuthorID是整数。 It is not an Option<int> . 它不是Option<int> Consequently, the pattern with two Some values is not compatible. 因此,具有两个Some值的模式不兼容。 You should have a pattern like this instead... 您应该改用这样的模式...

| Some itemName, itemAuthor ->

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

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