Maui_Ed
Maui_Ed

Reputation: 265

F# concatenate int array option to string

I have a data contract (WCF) with a field defined as:

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

I want to from the entries in the ExcludeList, create a comma separated string (to reduce the number of network hops to the database to update the status). I have tried the following 2 approaches, neither of which create the desired string, both are empty:

// 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 -> 
  ...     

Both compile and execute but neither give me anything in the string. Would greatly appreciate someone pointing out what I am doing wrong here.

Upvotes: 2

Views: 905

Answers (1)

Petr
Petr

Reputation: 4280

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

Output:

> 
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"

Upvotes: 2

Related Questions