c#比较两个数组的差异

xiaoxiao2021-02-28  93

#**c#比较两个数组的差异** --- 将DataTable中某一列数据直接转换成数组进行比较,使用的Linq,要引用命名空间using System.Linq; ```C# string[] arrRate = dtRate.AsEnumerable().Select(d => d.Field<string>("arry")).ToArray();//A string[] arrTemp = dtTemp.AsEnumerable().Select(d => d.Field<string>("arry")).ToArray();//B string[] arrUpd = arrRate.Intersect(arrTemp).ToArray();//相同的数据 (修改) string[] arrAdd = arrRate.Except(arrTemp).ToArray();//A中有B中没有的 (新增) string[] arrNew = arrTemp.Except(arrRate).ToArray();//B中有A中没有的 (删除) ``` 例子: ```C# string[] arrRate = new string[] { "a", "b", "c", "d" };//A string[] arrTemp = new string[] { "c", "d", "e" };//B string[] arrUpd = arrRate.Intersect(arrTemp).ToArray();//相同的数据 (结果:c,d) string[] arrAdd = arrRate.Except(arrTemp).ToArray();//A中有B中没有的 (结果:a,b) string[] arrNew = arrTemp.Except(arrRate).ToArray();//B中有A中没有的 (结果:e) ``` ---
转载请注明原文地址: https://www.6miu.com/read-27714.html

最新回复(0)