数据表到字符串
本文关键字:字符串 数据表 | 更新日期: 2023-09-27 18:33:27
我有一个三列的数据表
Id name value value1
我想从这个数据表创建一个字符串,以便
- 数据表中的每一行将创建两行
- 名称将变为一行
- 值 1 将成为另一行 如果名称和值 1
相似,则仅包含值 1,否则包括名称和值 1(此操作已完成)
如果一行的名称与任何其他行中的名称相似,则在字符串中两个重复行的前面添加以下文本
类似于 Id
这是我到目前为止写的:
Public Function GetGazetteer(dataTable As DataTable) As String
Dim processedData = New List(Of String)
Dim rowData = String.Empty
Dim results = New StringBuilder()
For Each row As DataRow In dataTable.Rows
processedData.Add(row(1))
If row(3) <> row(1) Then
processedData.Add(row(3))
End If
Next
For Each row As String In processedData
rowData = row
If rowData.Trim <> String.Empty Then
If (processedData.Where(Function(d) d = rowData).Count = 1) Then
results.Append(rowData)
results.Append("<br />")
Else
results.Append(rowData)
results.Append("*")
results.Append("<br />")
End If
End If
Next
Return results.ToString
End Function
目前,添加了*
(请建议如何添加上面的文本。
这是一个示例
id name value value1
1 this is string 1 abc This is sample
2 this is string 2 abc this is string 2
3 this is string 3 abc this is string 4
4 this is string 3 abc asasaasd
这是所需的输出
this is string 1
This is sample
this is string 2
*3 is duplicate of 4* this is string 3
this is string 4
*4 is duplicate of 3* this is string 3
asasaasd
根据问题给出的输出进行编辑
下面是使用 C# 的解决方案(希望您可以将其转换为 VB.NET)。我使用了一个结构来保存 id、值、col 信息。
结构为
public struct Container
{
public int Id;
public int Col;
public string Value;
}
方法是
public string GetGazetteer(DataTable dtInput)
{
string result = null;
List<Container> containers = new List<Container>();
List<Container> finalContainers = new List<Container>();
StringBuilder sb = new StringBuilder();
foreach (DataRow row in dtInput.Rows)
{
Container container;
container = new Container() { Id = int.Parse(row[0].ToString()), Value = row[1].ToString(), Col = 1 };
containers.Add(container);
if (row[1] != row[3])
{
container = new Container() { Id = int.Parse(row[0].ToString()), Value = row[3].ToString(), Col = 2 };
containers.Add(container);
}
}
containers = containers.OrderBy(c => c.Value).ThenBy(c => c.Id).ToList();
if (containers.Count > 0)
{
string initialVal = containers[0].Value;
finalContainers.Add(containers[0]);
for (int i = 1; i < containers.Count; i++)
{
if (containers[i].Value == initialVal)
{
finalContainers.Remove(containers[i]);
finalContainers.Remove(containers[i - 1]);
finalContainers.Add(new Container() { Id = containers[i - 1].Id, Value = "*" + containers[i - 1].Id + " is duplicate of " + containers[i].Id + "* " + containers[i - 1].Value });
finalContainers.Add(new Container() { Id = containers[i].Id, Value = "*" + containers[i].Id + " is duplicate of " + containers[i - 1].Id + "* " + containers[i].Value });
}
else
{
finalContainers.Add(containers[i]);
}
initialVal = containers[i].Value;
}
finalContainers = finalContainers.OrderBy(c => c.Id).ThenBy(c => c.Col).ToList();
foreach (Container container in finalContainers)
{
sb.Append(container.Value + "</br>");
}
result = sb.ToString();
}
return result;
}
这是我用于测试的示例数据表
DataTable dtInput = new DataTable();
dtInput.Columns.Add("id");
dtInput.Columns.Add("name");
dtInput.Columns.Add("value");
dtInput.Columns.Add("value1");
DataRow drInput1 = dtInput.NewRow();
drInput1[0] = "1";
drInput1[1] = "this is string 1";
drInput1[2] = "abc";
drInput1[3] = "This is sample";
dtInput.Rows.Add(drInput1);
DataRow drInput2 = dtInput.NewRow();
drInput2[0] = "2";
drInput2[1] = "this is string 2";
drInput2[2] = "abc";
drInput2[3] = "this is string 2";
dtInput.Rows.Add(drInput2);
DataRow drInput3 = dtInput.NewRow();
drInput3[0] = "3";
drInput3[1] = "this is string 3";
drInput3[2] = "abc";
drInput3[3] = "this is string 4";
dtInput.Rows.Add(drInput3);
DataRow drInput4 = dtInput.NewRow();
drInput4[0] = "4";
drInput4[1] = "this is string 3";
drInput4[2] = "abc";
drInput4[3] = "asasaasd";
dtInput.Rows.Add(drInput4);
输出将是
this is string 1</br>This is sample</br>this is string 2</br>*3 is duplicate of 4* this is string 3</br>this is string 4</br>*4 is duplicate of 3* this is string 3</br>asasaasd</br>