Question

I currently have a List(Of String) named docIds that looks a bit as follows:

{A3001,A40001,BF0003,13458}

it only contains letters and numbers. I would like to output that array as follows (for use in a DQL Query)

'A3001','A40001','BF0003','13458'

Off course i used the String.Join method

String.Join(",",docIds.ToArray())
Output: A3001,A40001,BF0003,13458

I know of 2 (non-performant) methods to add those quotes

  • Method 1: Before the String.Join, iterate every string in list and add the quotes.
  • Method 2: The following String operation:

        "'" + String.Join(",",docIds.ToArray()).Replace(",","','") + "'"
    

Question: Is there a more performant/Proper way to achieve the desired result?

Was it helpful?

Solution

You can use LINQ:

string.Join(",", docIds.Select(id => string.Format("'{0}'", id)));

This overload of String.Join was added in .NET 4.0, before you need to add ToArray to create a string[].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top