Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:57:22+00:00 2026-06-03T06:57:22+00:00

I have a datatable with three columns Id name value value1 I want to

  • 0

I have a datatable with three columns

Id name value value1

I want to create a string from this datatable so that

  1. each row in datatable will create two rows
  2. name will become one row
  3. value 1 will become another row
  4. If name and value1 are similar then include value 1 only otherwise include both name and value1 (this is done)

  5. 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:57:23+00:00Added an answer on June 3, 2026 at 6:57 am

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three columns in a datatable: string, DateTime, and decimal. I want to
I have two DataTables. First is DataTable NameAddressPhones = new DataTable(); with Three columns
I have a DataTable which I want to check if values in three of
I have a JSF datatable like this one: <h:form id=productsBox> <h:dataTable var=product value=#{categoriesBean.category.products} id=productsTable>
I have datatable with column name tag and 100 rows of data.I need to
i have a datatable with the results i just want to refine the results
I have a DataTable with 3 columns (a,b,c) and a docx file with the
I have a DataTable populated with values from a database. I have another datatable
I have a DataTable object. Every column is of type string. Using LINQ, how
I have a DataTable that I manually created and loaded with data using C#.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.