C#如何把两个datatable1与datatable2中不同的数据取出来,他们都有唯一标示符ID

2024-10-31 20:16:40
推荐回答(2个)
回答1:

        static void Main(string[] args)
        {
            DataTable datatable1, datatable2;
            var row1 = datatable1.Rows.Cast();
            var row2 = datatable2.Rows.Cast();
 
            var except1 = row1.Except(row2, new DataRowEqualityComparer());//1在2中不存在的
            var except2 = row2.Except(row1, new DataRowEqualityComparer());//2在1中不存在
 
            var except = except1.Union(except2).ToArray();//拼起来
        }
 
        class DataRowEqualityComparer : IEqualityComparer//只比较主键
        {
 
            public bool Equals(DataRow x, DataRow y)
            {
                return x["ID"].Equals(x["ID"]);
            }
 
            public int GetHashCode(DataRow obj)
            {
                return obj["ID"].GetHashCode();
            }
        }

回答2:

你是不是应该只把3号数据取出来保存?