我有一个包含三列的数据表
Id name value value1
我想从这个数据表中创建一个字符串,以便
如果name和value1相似,则只包含值1,否则包括name和value1(已完成)
如果一行的名称与任何其他行中的名称相似,则在字符串中添加以下两个重复行的文本
Id与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
答案 0 :(得分:1)
根据问题提供的输出进行编辑
这是使用C#的解决方案(希望您可以将其转换为VB.NET)。我使用了一个结构来保存id,value,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>