开发者

Comparing and Merging Identical columns of two Datatables

开发者 https://www.devze.com 2022-12-09 02:49 出处:网络
I have two datatables.There are few identical columns in both of them.Now I need to compare each Identical column\'s cell in both the datatables and build a third datatable by merging th开发者_StackOv

I have two datatables.There are few identical columns in both of them.Now I need to compare each Identical column's cell in both the datatables and build a third datatable by merging th开发者_StackOverflow社区e changes of identical column's cells and also the un-identical columns.Please help me with C# code in doing it.

Thanks, Vix


It can be done using the Merge() method of DataTable.

The merge method compares the key columns and merge the rows into one table.

// Set the identical columns to compare by in first table
table1.PrimaryKey = new DataColumn[]
                        { idColumnOfTable1, anotherIDColumnOfTable1 };
// Set the identical columns to compare by in second table
table2.PrimaryKey = new DataColumn[]
                        { idColumnOfTable2, anotherIDColumnOfTable2 };

// The MissingSchemaAction.Add will add the non-identical columns
// Non-identical columns existing in from table 2 will be added to table1
table1.Merge(table2, false, MissingSchemaAction.Add);
0

精彩评论

暂无评论...
验证码 换一张
取 消