WPF Comparing two datatables to find matching values

549 views Asked by At

enter code hereI have two data tables one of the data tables connects to sql server and the other connects to oracle.

I am running query statements on both and those work perfectly find.

Now I need to write something that will compare the "UNIT_NO" in oracle to the "VehicleName" in sql. yes they are the same number.

Right now the Oracle table bring in 6 columns and the SQL brings in 4 columns

an example would be:

VehicleName, VehicleGroupName, UserDefinedColumn2, UserDefinedColumn3

Unit_No, Unit_ID, Using_Dept, Status, Using_Dept_Desc,

I want my code to find the matching number from Unit_NO and VehicleName and display all the above information all in one row. I was thinking linq but I cant get it to display correctly

This code combines the columns from both tables but pulls but does not add the any data in the rows any suggest or fixes 
private void GetSQLOraclelinqData()
        {

            var TstarData = GetTrackstarTruckData();
            var M5Data = GetM5Data();

            DataTable ComTable = new DataTable();

            foreach (DataColumn OraColumn in M5Data.Columns)
            {
                ComTable.Columns.Add(OraColumn.ColumnName, OraColumn.DataType); 

            }
            foreach (DataColumn SQLColumn in TstarData.Columns)
            {
                if (SQLColumn.ColumnName == "VehicleName")
                    ComTable.Columns.Add(SQLColumn.ColumnName + 2, SQLColumn.DataType);
                else
                    ComTable.Columns.Add(SQLColumn.ColumnName, SQLColumn.DataType);

            }

            var results = TstarData.AsEnumerable().Join(M5Data.AsEnumerable(),
                a => a.Field<String>("VehicleName"),
                b => b.Field<String>("Unit_NO"),
                    (a, b) =>
                    {

                        DataRow row = ComTable.NewRow();
                        row.ItemArray = a.ItemArray.Concat(b.ItemArray).ToArray();
                        ComTable.Rows.Add(row);
                        return row;

                    });

            SQLDataTable.ItemsSource = ComTable.DefaultView;
            }
2

There are 2 answers

0
AMore On

my code is based off grabbing columns that are not populated yet in the datagrid I call two datatable on the code behind to populate the table and also us excel sheets. If anyone needs this information i can help.

I connect to SQL, Oracle and load excel sheets to make a comparison on data

5
Contango On

I would do it using two nested for loops.

The outer for loop would iterate through each row in the SQL DataTable.

The inner loop would iterate through each row in the Oracle DataTable, and if there is a match, it would store the match somewhere (perhaps in a list).

Optional Hints

  • Assuming that each number plate only occurs once, we could optimise this code by breaking out of the inner loop as soon as we get a match.
  • We cannot rely on the rows coming back in the same order. Thus, we cannot naively compare row 1 from SQL against row 1 from Oracle.