简体   繁体   English

如何获取类型 F# 中字段的 ID

[英]How to get a the ID of a field in a type F#

How do I create a function to search on a database and return just the ID of a type?如何创建一个函数来搜索数据库并仅返回类型的 ID?

Actually, I have a database, where I can search for a pair.实际上,我有一个数据库,可以在其中搜索一对。 The pair is a Type, in F#:这对是一个类型,在 F# 中:

module Types = 

    [<CLIMutable>]
    type Pair = {
        Id :int64
        PairName : string
    }

My objective is to create a function, that return just the value of the ID (int) given a PairName.我的目标是创建一个函数,该函数仅返回给定 PairName 的 ID (int) 的值。 I Already have a function that returns a Pair, given this name.我已经有一个函数返回一个 Pair,给定这个名字。

      module Query = 

        let GetSingleByName name = querySingleAsync<Types.Bird> {
            script "SELECT * FROM Pair WHERE PairName = @Name LIMIT 1"
            parameters (dict ["Name", box name])
        }

How can I return just the integer?我怎样才能只返回整数? Not all the Type.Pair?不是所有的 Type.Pair 吗?

let getIdByName name = querySingleAsync<int64> {
    script "SELECT Id FROM Pair WHERE PairName = @Name LIMIT 1"
    parameters (dict ["Name", box name])
}

if this does not work, you'd have to provide the definition of querySingleAsync .如果这不起作用,您必须提供querySingleAsync的定义。

Alternatively:或者:

let getIdByName name = async {
    let! single = getSingleByName name
    return single |> Option.map (fun pair -> pair.Id)
}

One of solutions that I find out was:我发现的解决方案之一是:

let GetSingleByName name = querySingleAsync<Types.Pair> {
                script "SELECT * FROM Pair WHERE PairName = @Name LIMIT 1"
                parameters (dict ["Name", box name])
            }



let GetIdByName (name:string)  = match GetSingleByName name |> Async.RunSynchronously  with
    | Some(pair)->  pair.Id
    | None -> 0 |> int64 



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

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