String.Join with String concatenation:Code Improvement

Use*_*999 1 .net c# string concatenation

我目前有一个看起来有点如下的List(Of String)名字docIds:

{A3001,A40001,BF0003,13458}
Run Code Online (Sandbox Code Playgroud)

它只包含字母和数字.我想输出该数组如下(用于DQL查询)

'A3001','A40001','BF0003','13458'
Run Code Online (Sandbox Code Playgroud)

当然我使用的String.Join方法

String.Join(",",docIds.ToArray())
Output: A3001,A40001,BF0003,13458
Run Code Online (Sandbox Code Playgroud)

我知道添加这些引号的2种(非性能)方法

  • 方法1:之前String.Join,iterate在列表中的每个字符串,并添加引号.
  • 方法2:以下String操作:

        "'" + String.Join(",",docIds.ToArray()).Replace(",","','") + "'"
    
    Run Code Online (Sandbox Code Playgroud)

问题:是否有更高效/正确的方法来达到预期效果?

Tim*_*ter 7

您可以使用LINQ:

string.Join(",", docIds.Select(id => string.Format("'{0}'", id)));
Run Code Online (Sandbox Code Playgroud)

String.Join在您需要添加ToArray以创建一个之前,在.NET 4.0中添加了此重载string[].