首页 > 代码库 > 比较两个DataTable的差集

比较两个DataTable的差集

 1       /// <summary>
 2         /// 获取dt1相对于dt2的增量
 3         /// </summary>
 4         /// <param name="dt1">比对的dt</param>
 5         /// <param name="dt2">参照的dt</param>
 6         /// <param name="differentType">差异说明</param>
 7         /// <param name="newDataTable">新的DataTable,主要获取DataTable 的架构</param>
 8         /// <returns>是否有差异</returns>
 9         private bool DataTableCompare(DataTable dt1, DataTable dt2, string differentType, ref DataTable newDataTable)
10         {
11             var normalReceiveAdd = dt1.AsEnumerable().Except(dt2.AsEnumerable(), DataRowComparer.Default);
12 
13             foreach (DataRow dr in normalReceiveAdd)
14             {
15                 var newdrArray = dr.ItemArray.ToList();
16                 if (newdrArray.Count(a => string.IsNullOrWhiteSpace(a.ToString())) == newdrArray.Count)
17                 {
18                     break;//去除空行
19                 }
20                 newdrArray.Add(differentType);
21                 newDataTable.Rows.Add(newdrArray.ToArray());
22             }
23             return newDataTable.Rows.Count > 0;
24         }

 

比较两个DataTable的差集