2

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.

0

1 Answer 1

2
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"
Sign up to request clarification or add additional context in comments.

2 Comments

I wouldn't use the Value property of an option from F#, that's mostly for C# use. match intoArray with Some items -> items |> Array.map string | None -> Array.empty
I see, the comma was causing the problem - sometimes the dumbest things waste so much time. Thank you Petr! And I agree w/ Joel's suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.