本文介绍了2 DataTable 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个 DataTable,我想创建第三个 DataTable,其中包含 DataTable 1 和 DataTable 2 之间的差异.

I have 2 DataTable and I want to create a third DataTable that contais the difference between DataTable 1 and DataTable 2.

比如DataTable1有原始数据,DataTable 2只是一个副本,就像复制一样.但是当您在 DataTable1 中插入新行时,DataTable2 刚刚插入了同一行.现在我的代码在DataTable1和DataTable2之间做一个比较,如果不相等(插入了1行或更多),DataTable2再次记录DataTable1中的所有数据.

For example, DataTable1 has the original data, and the DataTable 2 is just a copy, like a replication. But when you insert a new row in DataTable1, the DataTable2 has just insert the same row. Nowaday my code do a compare between DataTable1 and DataTable2, if not equals (1 row or more was inserted), DataTable2 record all data from DataTable1 again.

我怎样才能做一个选择命令,做这个区别并将这些数据记录在第三个 DataTable 中?

How can I do a select command, that do this difference and record those datas in a third DataTable ?

推荐答案

我会考虑有两列来标识表(col1,col2)

I will consider that there are two columns to identify the tables(col1,col2)

var rowsOnlyInDt1 = dt1.AsEnumerable().Where(r => !dt2.AsEnumerable()
                    .Any(r2 => r["col1"].Trim().ToLower() == r2["col1"].Trim().ToLower() && r["col2"].Trim().ToLower() == r2["col2"].Trim().ToLower()));

DataTable result = rowsOnlyInDt1.CopyToDataTable();//The third table

这篇关于2 DataTable 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 18:23