【发布时间】:2019-09-24 21:38:43
【问题描述】:
我正在尝试解析 DataTable 行中的逗号分隔值并从中创建单独的行。
这是我开始使用的表格示例:
ID Date Places
1 09/24/2019 Paris,Tokyo,Rome
2 09/23/2019 London,Florence,Barcelona
3 09/22/2019 Vienna,Rome,London
我的输出数据表应该是这样的:
ID Date Places
1 09/24/2019 Paris
1 09/24/2019 Tokyo
1 09/24/2019 Rome
2 09/23/2019 London
2 09/23/2019 Florence
2 09/23/2019 Barcelona
3 09/22/2019 Vienna
3 09/22/2019 Rome
3 09/22/2019 London
到目前为止,这是我的代码:
for (int i = 0; i < dataTable.Rows.Count; i++)
{
string[] places = dataTable.Rows[i][2].ToString().Split(',');
if (places.Length > 1)
{
foreach (string s in places)
{
//create a new datarow
//get the values for row[i] (ID and Date)
//assign the place
}
}
}
我需要foreach 的帮助。
【问题讨论】:
-
您需要第二个 DataTable,其中只有 ID、Date 和 Place 列并添加到其中。