Efficient search

Hi,

I have two tables that I create to display information in a DataGridView Control. Now I need to compare the fields of these two tables. It is somewhat like:

    void SomeFunction()
    {
        for (int i = 0; i < tableA.Rows.Count ; i++)
        {
            DataRow rowA = tableA.Rows[ i ]  ;
            for (int j = 0; j < tableB.rows.count ; j++)
            {
                DataRow rowB = tableB.Rows[ j ];
                if (rowA["Col1"].ToString().Equals(rowB["Col1"].ToString()))
                    DoSomeProcessing();
            }
        }
    }

I am sure there are much more better and efficient ways to perform this comparison. I would appreciate if I can get some really good suggestions.

Thanks!

 



Answer this question

Efficient search

  • Echo

    doing what exactly in SQL

  • bklambdin

    I am not working with a database. I create the tables as needed to display the data in DataGridView.
  • Doug DeBug

    How about doing it in the database SQL is pretty good at this :)

  • Darren Grove

    cleaner code perhaps (untested):



    foreach(DataRow currentRowA in tableA.Rows)
    {
    foreach(DataRow currentRowB in tableB.Rows)
    {
    if (currentRowA["Col1"].ToString().Equals(currentRowB["Col1"].ToString()))
    {
    DoSomeProcessing();
    }
    }
    }

    in terms of efficiency....will have a think about that. If you are going to be comparing row by row by column then this would be the way I believe.



  • Efficient search