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

GD_GRAYAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GD_GRAYAuthor Commented:
This code works like a champ...  !

DataTable targetTable = OrderTable.Clone();
                var dt2Columns = custTable.Columns.OfType<DataColumn>().Select(dc =>
                new DataColumn(dc.ColumnName, dc.DataType, dc.Expression, dc.ColumnMapping));
                var dt2FinalColumns = from dc in dt2Columns.AsEnumerable()
                                      where targetTable.Columns.Contains(dc.ColumnName) == false
                                      select dc;
                targetTable.Columns.AddRange(dt2FinalColumns.ToArray());
                var rowData = from row1 in cDataTable.AsEnumerable()
                              join row2 in custDataTable.AsEnumerable()
                              on row1.Field<string>("CompanyID") equals row2.Field<string>("CompanyID")
                              select row1.ItemArray.Concat(row2.ItemArray.Where(r2 => row1.ItemArray.Contains(r2) == false)).ToArray();
                foreach (object[] values in rowData)
                    targetTable.Rows.Add(values);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.