我的代码如下:
List<string> colorList = new List<string>();
....
sCombo = reader["Combo"].ToString();
colorList.Add(sCombo.ToString());
....
foreach (var Combo in colorList)
{
Response.Write(string.Join(",", Combo));
}
输出:D410D430D440D420
而不是D410,D430,D440,D420
将List<string>
转换为逗号分隔的字符串的最简单方法是什么?
编辑#01
您的建议有效,但是我需要这个新输出:
'D410','D430','D440','D420'
因为在SQL查询中使用此string
。
谢谢
答案 0 :(得分:6)
我认为这将非常方便
var colorList = new List<string>() { "D410", "D430", "D440", "D420" };
string commaSeparated = string.Join(",", colorList);
Console.WriteLine(commaSeparated);
或尝试基于Linq的解决方案
Console.WriteLine(colorList.Select(s => s + ",").Aggregate((s, q) => s + q).TrimEnd(','));
输出
D410,D430,D440,D420
修改
string result = string.Join(",", colorList.Select(e => "'" + e + "'"));
Console.WriteLine(result);
会给您
'D410','D430','D440','D420'
答案 1 :(得分:4)
没有ConfigurationManager.AppSettings
:
foreach
答案 2 :(得分:1)