How do you take two datatables and match values, then add something to a column in the matched row values?

StackOverflow https://stackoverflow.com/questions/11182399

Вопрос

I have two datatables and I need to match zipcodes from each of the datatables, and every time the program finds a match, I need to set a column in the matched rows to a string. Here is my code.

I do not know what to do in the foreach loop, or if there is an easier way to do this, please say it.

        //Datatable for entire list of zipcodes
        DataTable datat = new DataTable("DATA");
        DataColumn state = new DataColumn();
        DataColumn county = new DataColumn();
        DataColumn zipcode = new DataColumn();
        DataColumn latitude = new DataColumn();
        DataColumn longitude = new DataColumn();
        DataColumn salesperson = new DataColumn();
        DataColumn originalrec = new DataColumn();

        //Add the columns to the datatable
        datat.Columns.Add(state);
        datat.Columns.Add(county);
        datat.Columns.Add(zipcode);
        datat.Columns.Add(latitude);
        datat.Columns.Add(longitude);
        datat.Columns.Add(salesperson);
        datat.Columns.Add(originalrec);

        return datat;
    }

    private static DataTable InitData2()
    {
        //Datatable for entire list of zipcodes
        DataTable datat2 = new DataTable("DATA");
        DataColumn ctype = new DataColumn();
        DataColumn csalesperson = new DataColumn();
        DataColumn czipcode = new DataColumn();

        //Add the columns to the datatable
        datat2.Columns.Add(ctype);
        datat2.Columns.Add(csalesperson);
        datat2.Columns.Add(czipcode);

        return datat2;
    }       

    private static String InitPath()
    {
        string path = "C:/Documents and Settings/Andre/Desktop/SalesMap/data.csv";
        return path;
    }

    private static String InitPath2()
    {
        string path2 = "C:/Documents and Settings/Andre/Desktop/SalesMap/CUSTOMER_PROSPECT_SALESPERSON.csv";
        return path2;
    }

    public static void Main()
    {
        try
        {
            DataTable dt = InitData1();
            DataTable dt2 = InitData2();
            StreamReader sr = new StreamReader(InitPath());
            StreamReader sr2 = new StreamReader(InitPath2());
            String csvData = string.Empty;
            String csvData2 = string.Empty;

            while ((csvData = sr.ReadLine()) != null)
            {
                String[] data = csvData.Split(',');
                //dt.Rows.Add(data);
                DataRow newRow1 = dt.NewRow();
                newRow1[0] = data[0].ToString();
                newRow1[1] = data[1].ToString();
                newRow1[2] = data[2].ToString();
                newRow1[3] = data[3].ToString();
                newRow1[4] = data[4].ToString();
                newRow1[5] = "";
                newRow1[6] = "";
                dt.Rows.Add(newRow1);

                Console.WriteLine("Row added for: dt");
            }
            dt.WriteXml(@"c:\test\dt1.xml");
            sr.Close();

            while ((csvData2 = sr2.ReadLine()) != null)
            {
                String[] data2 = csvData2.Split(',');
                DataRow newRow2 = dt2.NewRow();
                newRow2[0] = data2[0].ToString();
                newRow2[1] = data2[1].ToString();
                newRow2[2] = data2[2].ToString();
                dt2.Rows.Add(newRow2);

                Console.WriteLine("Row added for: dt2");
            }

            dt2.WriteXml(@"c:\test\dt2.xml");
            sr2.Close();

            foreach (DataRow row1 in dt.Rows)
            {
                //Dont know what to do here
            }
        }

        catch (Exception e)
        {
            //Console.WriteLine("The files could not be read:");
            //Console.WriteLine(e.Message);
            StreamWriter sw = new StreamWriter(@"c:\test\error.txt");
            sw.WriteLine(e.Message);
            sw.Close();


        }
    }
}
}
Это было полезно?

Решение

Not sure if i understood at all. But as i can imagine the look it should be like this:

            DataTable dtSampleOne = new DataTable();
            DataTable dtSampleTwo = new DataTable();

            foreach (DataRow rowSampleOne in dtSampleOne.Rows)
            {
                string zipCodeSampleOne = rowSampleOne["zipCodeToMatchOne"].ToString();

                foreach (DataRow rowSampleTwo in dtSampleTwo.Rows)
                {
                    if (rowSampleTwo["zipCodeToMatchTwo"].ToString().Equals(zipCodeSampleOne))
                    {
                        // Do your stuff here
                    }                            
                }
            }

I'll keep in touch if you need anything.

Другие советы

The easiest way would be to do it all in the stored procedure. So your stored procedure does a simple match (probably use intersect) and returns result. If you cannot use stored procedures, I would then select both sets and load in into Lists, then you can use LINQ commands to do Intersect. No need for foreach loops.

List<string> list1 = // load list from db
List<string> list2 = // load second list from db

var result = list1.Intersect(list2);

This is it

Using LINQ, we can further improve the code given by Luis Hernández though it does the job.

In the given solution (Luis Hernández), we have 2 loops. Assume SampleOne as 100 rows and SampleTwo as 200 rows. We will end up with looping 100 X 200 times!

Instead, we can get the matching records, and we can loop through with only those records. This would avoid unnecessary loops.

        var sampleOne = new DataTable();
        var sampleTwo = new DataTable();

        // TODO : I assume these two table have the common column "ZipCode".
        // TODO : Add your sample data here!

        var matchingRows = from s1 in sampleOne.AsEnumerable()
                           join s2 in sampleTwo.AsEnumerable() on s1.Field<string>("ZipCode") equals s2.Field<string>("ZipCode")
                           select s1;

        foreach (var row in matchingRows)
        {
            // Do your stuff here !
        }

Please don't hesitate to inform me If my solution is not up to the mark!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top