I have two DataTables both has same no of columns and column names.Am in need of comparing both for the different rows.Which means even if one cell doesnt match the row should be plotted.I tried with

table1.Merge(table2);
DataTable modified = table2.GetChanges();

But this is returning null.

Where as

IEnumerable<DataRow> added = table1.AsEnumerable().Except(table2.AsEnumerable());

This is returning the table1 values alone even there are different values for a some cells in table1 compared to table2.

Can anyone help me for this comparison.Various sites i referred,the instruction said was to compare each column in a row but since i have N no of columns i cant go with that.I need a smarter way of comparison which would be efficient.

Thanks in advance

有帮助吗?

解决方案

IEnumerable<DataRow> added = table1.AsEnumerable().Except(table2.AsEnumerable());

should be changed to

IEnumerable<DataRow> added = table1.AsEnumerable().Except(table2.AsEnumerable(),DataRowComparer.Default);

Because DataRows don't know how to compare themselves to eachother on their own. You can also provide your own equality delegate instead of DataRowComparer.Default if required.

其他提示

Have you tried using merge? http://msdn.microsoft.com/en-us/library/fk68ew7b.aspx

Datatable1.Merge(datatable2);
DataTable DataTable3 = Datatable2.GetChanges();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top