Checking identical ID's in Dual Loop

doramail05
doramail05 used Ask the Experts™
on
i have to compare two tables with each so_order_no the same, how do we compare the so_order_no in a dual loop?

below are the code which is not comparing coz the i loop outside the existing row before j finished all its loop.
for (int i = 0; i < dsTable1.Tables[0].Rows.Count; i++)
{
        for (int j = 0; j < dsTable2.Tables[0].Rows.Count; j++)
        {
                if (dsTable1.Tables[0].Rows[ i ]["so_order_no"].ToString() != dsTable2.Tables[0].Rows[ j ].ToString())
                {
                               //proceed
                }
         }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The way you have done it in your code is how I would expect it to work.  
What is happening when you run your code?

Also, have you considered doing this in SQL, in your database layer?  The semantics above are likely achievable with an appropriate JOIN.

J.

Author

Commented:
for example if int _i, and int _j put int to detect,

1st loop                                                              2nd loop
---------                                                              -----------
i                           j                                         i                         j
20                      20                                      20                     30

for the 2nd loop, it will by pass the 'if' statement. For my scenario, i would like to get 'i' 2nd row as 30,
so the 2nd loop is like  30   n    30 .    For the dual loop, it will not achieve this,

if it is suggested a JOIN statement, 1 table is in database and 2nd table is in the other database.
could we use array to store it?


for (int i = 0; i < dsTable1.Tables[0].Rows.Count; i++)
{
        for (int j = 0; j < dsTable2.Tables[0].Rows.Count; j++)
        {
              int _i = (int)dsTable2.Tables[0].Rows[ i ]["so_order_no"];
              int _j = (int)dsTable2.Tables[0].Rows[ j ]["so_order_no"];
 
                if (dsTable1.Tables[0].Rows[ i ]["so_order_no"].ToString() != dsTable2.Tables[0].Rows[ j ]["so_order_no"].ToString())
                {
                               //proceed
                }
         }
}

Open in new window

Author

Commented:
i was thinking about the below, with arraylist:
but i dunno what to put it in the ' ? '
public static ArrayList _i_array = new ArrayList[ ? ]

as they are declared outside the class / main load

will the arraylist be working? kindly advise
public static ArrayList _i_array = new ArrayList[ ? ] 
...
for (int i = 0; i < dsTable1.Tables[0].Rows.Count; i++)
{
      _i_array[i] = dsTable1.Tables[0].Rows[i]["so_order_no"];
}
 
for (int j = 0; j < dsTable1.Tables[0].Rows.Count; j++)
{
          foreach (int _j_al in _i_array)
          {     
               if (_j_al != dsTable1.Tables[0].Rows[j][so_order_no"])
               {
                      // would like to proceed if both so_order_no is not identical. 
               }
}

Open in new window

CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

I still cannot see anything wrong with your first approach.  Maybe I am confused about what you are trying to do.  Your code should enter the if block in all instances where the string representation of a value in dsTable1 does not match the string representation of a value in dsTable2.

In your second code, you are casting the values extracted from dsTable1 and dsTable2 to ints (_i and _j).  You do not appear to be comparing these casted values, though.  Is it possible that the string representations of the values in dsTable1 and dsTable2 are equal (e.g. a type name)?

Try the following:


for (int i = 0; i < dsTable1.Tables[0].Rows.Count; i++)
{
    for (int j = 0; j < dsTable2.Tables[0].Rows.Count; j++)
    {
        int _i = (int)dsTable2.Tables[0].Rows[i]["so_order_no"];
        int _j = (int)dsTable2.Tables[0].Rows[j]["so_order_no"];
 
        if (_i != _j)
        {
            //proceed
        }
    }
}

Open in new window

As for JOINing across two separate database, it will depend on your database provider.  In SQL Server, you can JOIN across two databases on the same instance, using three-part notation; or across two databases on different instances using an OPENQUERY to a linked server object.  Similar methods can be used in Oracle, with schemas or DBLINKs.
i figured out oredi,

by assigning the public static int totalarray = 0 , and the totalarray into ArrayList total .
public ArrayList alist = new ArrayList(totalarray);
public static int totalarray = 0;
...
using (SqlConnection sqlsourceconn1 = new SqlConnection(sourceconnstring))
            {
 
                try
                {
                    sqlsourceconn1.Open();
 
                    string strSQLSelectONEU = "USE RetailDB; Select * FROM ONEU_Sales_Order";
 
                    DataSet dsSelectONEU = new DataSet();
 
                    SqlDataAdapter adapterSelectONEU = new SqlDataAdapter(strSQLSelectONEU, sqlsourceconn1);
                    adapterSelectONEU.Fill(dsSelectONEU);
 
                    totalarray = dsSelectONEU.Tables[0].Rows.Count;
 
                    for (int i = 0; i < dsSelectONEU.Tables[0].Rows.Count; i++)
                    {
                        alist.Add(Convert.ToInt32(dsSelectONEU.Tables[0].Rows[i]["sale_order_no"]));
                    }
 
                    using (SqlConnection sqlprontoconn1 = new SqlConnection(prontoconnstringextract))
                    {
                        sqlprontoconn1.Open();
 
                        string strSQLselectSO = "USE ProntoDemo; Select * FROM sales_order WHERE so_order_date ='" + DateTime.Today.Date + "' AND LEFT(so_order_no, 3) = " + outlet_id;
 
                        DataSet dsselectSO = new DataSet();
 
                        SqlDataAdapter adapterselectSO = new SqlDataAdapter(strSQLselectSO, sqlprontoconn1);
                        adapterselectSO.Fill(dsselectSO);
 
                        for (int j = 0; j < dsselectSO.Tables[0].Rows.Count; j++)
                        {
 
                            foreach (int _alist in alist)
                            {
                                if (_alist == Convert.ToInt32(dsselectSO.Tables[0].Rows[j]["so_order_no"]))
                                {
                                    listBox1.Items.Add(dsselectSO.Tables[0].Rows[j]["so_order_no"].ToString() + " is the same");
                                }
                            }
 
                        }
 
                    }
 
                }
                catch (Exception ex)
                {
                    listBox1.Items.Add(ex.Message.ToString());
                }
                finally
                {
                    sqlsourceconn1.Close();
                }
 
            }

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial