数据表以逗号分隔的值 [英] data table comma separated values
本文介绍了数据表以逗号分隔的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据表。
我有两个列,如MSG和TO。
对于单个消息..我有多个 记录。
例如:
MSG TO
test akshay < br $> b $ b测试同一个人
测试sidd
嗨som
现在使用对于每个循环我希望所有TO以逗号分隔。
例如:
MSG TO
测试akshay,同样,sidd
嗨som。
我怎样才能得到这个?
谢谢-Akshay
I have a data table.
In that I have two columns like "MSG" and "TO".
For a single message ..I have multiple "TO" records.
For eg:
MSG TO
test akshay
test sameer
test sidd
Hi som
Now using for each loop i want all "TO" as comma separated.
for eg:
MSG TO
test akshay,sameer,sidd
Hi som.
How can i get this??
Thanks-Akshay
推荐答案
你需要两个foreach循环:一个用于收集值,另一个用于输出值。
You need two foreach loops: One to collect the values, and one to output them.
Dictionary<string, StringBuilder> dict = new Dictionary<string, StringBuilder>();
foreach (DataRow row in dt.Rows)
{
string msg = (string)row["MSG"];
string to = (string)row["TO"];
if (!dict.ContainsKey(msg))
{
dict.Add(msg, new StringBuilder(to));
}
else
{
dict[msg].AppendFormat(",{0}", to);
}
}
foreach (string key in dict.Keys)
{
Console.WriteLine("{0} {2}", key, dict[key].ToString());
}
这篇关于数据表以逗号分隔的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文