0

As question How to match the 2 column of item by refering to another txt file in C#. For example, currently I'm doing the comparison by matching one of the column in both txt file. If the id of 1st txt file equals to 2nd file then perform my operation.

But the problem is both of the have the different id but refering to same things.

How do I take the value in the 1st txt file by searching with the another new created txt file which have the following reference data

reference_file:
    1,AAA
    2,BBB
    3,CCC

Example of 1st txt file 1st column

info1,info2,info3,info1,2,info4,info5,info6,info7

Example of 2nd txt file 1st column

info1,info2,info3,info1,bbb,info4,info5,info6,info7

If the column in 1st txt file is 2 then the value is BBB

Currently one of the line will do this matching if both of the column id with same value.

    static void Main(string[] args)
    {

        DataTable dt_1sttxtfile = ConvertToDataTable(@"C:\Users\manchunl\Desktop\1sttxtfile.txt", 10);
        DataTable dt_2ndtxtfile = ConvertToDataTable(@"C:\Users\manchunl\Desktop\2ndtxtfile.txt", 10);
        DirectoryInfo reference_file = new DirectoryInfo(@"C:\Users\manchunl\Desktop\Reference.txt");

        foreach (DataRow row1 in dt_1sttxtfile.Rows)
        {
            foreach (DataRow row2 in dt_2ndtxtfile.Rows)
            {
                var 1sttxtfileRow = row1.ItemArray;
                var 2ndtxtfileRow = row2.ItemArray;

                if (1sttxtfileRow[4].Equals(2ndtxtfileRow[4]))
                {
                   //Perform my task
                }
             }
        }
   }

Sorry for bad explanation if my explaination not clear.

apple orange
  • 35
  • 3
  • 8

1 Answers1

0

If I understand you problem correctly, Let me give you pseudo code which you can extend to fit your need. Lets assume I have your three variables like below

 Dictionary<string, string> reference_File = new Dictionary<string, string>() { { "1", "AAA" }, { "2", "BBB" }, { "3", "CCC" } };
 List<string> dt_1sttxtfile = new List<string>() { "info1", "info2", "info3", "info1", "2", "info4", "info5", "info6", "info7" };
 List<string> dt_2ndtxtfile = new List<string>() { "info1", "info2", "info3", "info1", "bbb", "info4", "info5", "info6", "info7" };

To perform comparison column by column between dt_1sttxtfile and dt_2ndtxtfile taking reference of reference_File, you can do something like below

int columnCount = dt_1sttxtfile.Count;
for (int column = 0; column < columnCount; column++)
{
       if (reference_File.TryGetValue(dt_1sttxtfile[column], out var referenceValue))
         {
           if (string.Compare(referenceValue, dt_2ndtxtfile[column], StringComparison.InvariantCulture) == 0)
               {
                   // perform your task
               }
          }
  }

The above was just the pseudo code to propagate the idea. If you want to extend this in your scenario where you have dt_1sttxtfile and dt_2ndtxtfile as data table you can do something like below.

            DataTable dt_1sttxtfile = ConvertToDataTable(@"C:\Users\manchunl\Desktop\1sttxtfile.txt", 10);
            DataTable dt_2ndtxtfile = ConvertToDataTable(@"C:\Users\manchunl\Desktop\2ndtxtfile.txt", 10);
            Dictionary<string, string> reference_File = new Dictionary<string, string>() { { "1", "AAA" }, { "2", "BBB" }, { "3", "CCC" } };
            int columnCount = dt_1sttxtfile.Columns.Count;
            foreach (DataRow row1 in dt_1sttxtfile.Rows)
            {
                foreach (DataRow row2 in dt_2ndtxtfile.Rows)
                {
                    for (int column = 0; column < columnCount; column++)
                    {
                        if (reference_File.TryGetValue(row1[column].ToString(), out var referenceValue))
                        {
                            if (string.Compare(referenceValue, row2[column].ToString(), StringComparison.InvariantCulture) == 0)
                            {
                                // perform your task
                            }
                        }
                    }
                }
            }

please be noted that it would be easier if you could convert your reference_file to dictionary of . I am not sure why it is mentioned as DirectoryInfo in your sample code(I assume it was typo). you can take reference the following link Transform a DataTable into Dictionary C# to transform reference_file (once read into datatable) to Dictionary

Asif
  • 329
  • 1
  • 7
  • How to compare it if my dt_1sttxtfile and dt_2ndtxtfile is store in the txt file which I get read it as the dataTable – apple orange Mar 21 '19 at 12:12
  • That is why I mentioned it as a pseudo code, which is to give an idea about what could be the possible approach. if you want to extend this to your solution where you have 'dt_1sttxtfile' and 'dt_2ndtxtfile' as datatable you can think of something like what I edited. – Asif Mar 21 '19 at 12:42