문제

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?

도움이 되었습니까?

해결책

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[].

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top