arrays - F# 将 int 数组选项连接到字符串

标签 arrays f#

我有一个数据协定 (WCF),其字段定义为:

[<DataContract(Namespace = _Namespace.ws)>]
type CommitRequest =
{
// Excluded for brevity
...
[<field: DataMember(Name="ExcludeList", IsRequired=false) >]
ExcludeList : int array option
}

我想从 ExcludeList 中的条目创建一个逗号分隔的字符串(以减少到数据库更新状态的网络跳数)。我尝试了以下两种方法,这两种方法都没有创建所需的字符串,都是空的:

// Logic to determine if we need to execute this block works correctly
try
   //  Use F# concat
   let strList = request.ExcludeList.Value |> Array.map string
   let idString = String.concat ",", strList
   // Next try using .NET Join
   let idList = String.Join ((",", (request.ExcludeList.Value.Select (fun f -> f)).Distinct).ToString ())
with | ex -> 
  ...     

都编译并执行,但都没有给我字符串中的任何内容。非常感谢有人指出我在这里做错了什么。

最佳答案

let intoArray : int array option = Some [| 1; 23; 16 |]
let strList = intoArray.Value |> Array.map string
let idString = String.concat "," strList // don't need comma between params
// Next try using .NET Join
let idList = System.String.Join (",", strList) // that also works

输出:

> 
val intoArray : int array option = Some [|1; 23; 16|]
val strList : string [] = [|"1"; "23"; "16"|]
val idString : string = "1,23,16"
val idList : string = "1,23,16"

关于arrays - F# 将 int 数组选项连接到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29806745/

相关文章:

C:二维数组与普通数组相同大小的区别

php - 将多维数组变成一维数组

c - 反对 static int 指针的参数

c# - 如何通过 System.Data.SQLite 检索 F# 查询表达式的 SQL 查询字符串?

F# 为二叉搜索树制作 ToString 方法

java - 如何将数组中的项目放入不同的复选框中?

php - 返回数组第一个重复元素的最佳方法

f# - 有没有一种不太笨拙的方法来执行无限序列的自动编号?

F#如何处理这个递归的相互类方法

function - 如何从 F# 函数中获取工作变量