using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { DataTable dt = new DataTable(); dt.Columns.Add("Age",typeof(string)); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("Address", typeof(string)); DataRow dA = dt.NewRow(); dA["Age"] = "45"; dA["Name"] = "王五"; dA["Address"] = "BBBBBBBBBbbbbbbbb"; dt.Rows.Add(dA); DataRow dA1 = dt.NewRow(); dA1["Age"] = "123"; dA1["Name"] = "张三"; dA1["Address"] = "河南省济源市"; dt.Rows.Add(dA1); DataRow dA2 = dt.NewRow(); dA2["Age"] = "123"; dA2["Name"] = "张三"; dA2["Address"] = "河南省济源市"; dt.Rows.Add(dA2); //再复制一个DataTable和上面的DataTable结构一样 DataTable dt1 = dt.Clone(); //添加数据 DataRow dr1 = dt1.NewRow(); dr1["Age"] = "123"; dr1["Name"] = "张三"; dr1["Address"] = "河南省济源市"; dt1.Rows.Add(dr1); DataRow dr2 = dt1.NewRow(); dr2["Age"] = "45"; dr2["Name"] = "王五"; dr2["Address"] = "BBBBBBBBBbbbbbbbb"; dt1.Rows.Add(dr2); DataRow dr3 = dt1.NewRow(); dr3["Age"] = "56"; dr3["Name"] = "赵六"; dr3["Address"] = "FSDREWGF@!$#@!@#"; dt1.Rows.Add(dr3); //将两个DataRow连接起来,当然有多少个DataTable就可以连接多少 var KV= dt.Rows.Cast<DataRow>().Concat(dt1.Rows.Cast<DataRow>()).GroupBy((dr) => { //用列数据当键名 //只有这些列值全部相等才会一样 return new { Age= dr["Age"].ToString(), Name=dr["Name"].ToString(), Address=dr["Address"].ToString() }; }).Where(V=>V.Count()>1); //打印出 找到的重复数据 foreach (var item in KV) { Console.WriteLine(item.Key.Name); } } } }
如上面,我建了二个DataTable,其中 张三,和王五分别分布在二个DataTable中。当然你建多少个都可以
不好意思,昨天中午忙着赶这功能,最后直接用了三层循环做了。多谢帮助~