Reputation: 27996
I have a datatable with three columns
Id name value value1
I want to create a string from this datatable so that
If name and value1 are similar then include value 1 only otherwise include both name and value1 (this is done)
If name of one row is similar to name in any other row, then add following text infront of both duplicate rows in string
Id is similar to Id
This is what I have written so far:
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
Currently, *
is added ( please suggest how to add above text.)
Here is a sample
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
Here is desired output
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
Upvotes: 0
Views: 1540
Reputation: 4361
Edited based on output given on question
Here is a solution using C# (hope you can convert this to VB.NET). I have used a struct to hold id, value, col information.
The struct is
public struct Container
{
public int Id;
public int Col;
public string Value;
}
The method is
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;
}
This is the sample datatable I used for testing
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);
output will be
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>
Upvotes: 1