Link to home
Start Free TrialLog in
Avatar of GD_GRAY
GD_GRAYFlag for United States of America

asked on

Merging Data Tables with common Key ID

I have two data tables, one with say 200 rows of today's order info and another table with 5000+ rows of company info. Each table has a column named "CompanyID". What I need to do is merge or build a new table with the Company Info onto the same row as the Order with the same "CompanyID".  Can anyone help with this ? I found this code but it is not working.

void MergeColumns(DataTable destination, DataTable source, IEnumerable<string> columnsToSkip)
        {
            if (columnsToSkip == null) columnsToSkip = new List<string>();
            foreach (var col in source.Columns.OfType<DataColumn>().Where(col => !columnsToSkip.Contains(col.ColumnName)))
            {
                var newCol = destination.Columns.Add(col.ColumnName, col.DataType);
                newCol.AllowDBNull = true;
                destination.Rows[0][newCol] = source.Rows[0][col];
            }
        }

Open in new window


MergeColumns(custTable, orderTable, new string[] { "CompanyID" });

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of GD_GRAY
GD_GRAY
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial