我有一个将数据返回到数据表的选择语句。大约 3-5 列和 5-10 行。我正在尝试从“电子邮件”列收集所有数据并将其添加到单行字符串中。
结果:
[email protected];[email protected];[email protected];[email protected]
最佳答案
解决方案1
迭代每个
DataRow
并从DataColumn
获取值.将值添加到数组/列表中。
使用
String.Join()
将数组/列表转换为字符串与;
作为分隔符。
using System.Collections.Generic;
List<string> emails = new List<string>();
foreach (DataRow row in dt.Rows)
{
emails.Add(row["Email"].ToString());
}
string result = String.Join(";", emails);
解决方案2
使用 System.Linq 获取电子邮件
List<string>
.使用
String.Join()
将数组/列表转换为字符串与;
作为分隔符。
using System.Linq;
string result = String.Join(";", dt.AsEnumerable()
.Select(x => x["Email"].ToString())
.ToList());
关于c# - 将 DataTable 中的列结果获取为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72751445/