0

3列のデータテーブルがあります

Id name value value1

このデータテーブルから文字列を作成して、

  1. データテーブルの各行は 2 つの行を作成します
  2. 名前が1行になる
  3. 値 1 は別の行になります
  4. name と value1 が類似している場合は、値 1 のみを含めます。それ以外の場合は、name と value1 の両方を含めます (これは行われます)。

  5. ある行の名前が他の行の名前と似ている場合は、文字列内の両方の重複行の前に次のテキストを追加します

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
4

1 に答える 1

1

質問に与えられた出力に基づいて編集

C# を使用したソリューションを次に示します (これを VB.NET に変換できることを願っています)。ID、値、列情報を保持するために構造体を使用しました。

構造体は

    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>
于 2012-05-05T18:28:44.807 に答える