两个集合求差

需求:从两个不同历史版本的数据库提取出相同的表数据(多个表)进行差异对比。

实施:使用集合的Except扩展方法

过程:开始想的是写代码循环比较差异,但是代码写起来比较繁琐,后来发现集合有一个Except扩展方法正是用来实现求差集的。

部分代码如下

  EnumerableRowCollection<HtRefInfo> hta;
  EnumerableRowCollection<HtRefInfo> htb;
  hta = DalA.LoadHt(ids);
  htb = DalB.LoadHt(ids);
  var only_a_has = hta.Except(htb, new HtDiffEquality());//a有,b没有的

  //----------------------------------------------------------------------------

  HtDiffEquality类里定义了两个集合的比较规则
  public class HtDiffEquality : IEqualityComparer<HtRefInfo>
    {
        public bool Equals(HtRefInfo x, HtRefInfo y)
        {
            return (x.TableName == y.TableName && x.TableID == y.TableID && x.HtID == y.HtID);
        }

        public int GetHashCode(HtRefInfo obj)
        {
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return obj.ToString().GetHashCode();
            }
        }
    }

注意:如果两个集合的数据比较多,程序会比较慢。我实际的数据两个集合都有10万条数据,执行了10几分钟(电脑8g内存,I5)

补充:除了except扩展方法,还有Intersect(求交集),Union(求并集)等。

后期:这个扩展内部大概还是用循环写的吧,以后有时间要看看源码

时间: 2024-08-04 17:43:54

两个集合求差的相关文章

C# 两个集合比较差值 Linq的Except的用法

C# 两个集合比较差值 Linq的Except的用法 值类型的集合比较差值 List<string> strList1 = new List<string>(){"a", "b", "c", "d"}; List<string> strList2 = new List<string>() { "a", "b", "f",

两个集合求对称差集

对称差集,两个集合的并集,减去交集. 比如,集合1,2,3和集合3,3,4,5的对称差是集合1,2,4,5. 想到的解法,将两个排序,两个集合分别用两个工作指针i,j.比较两个指针指向的元素,相同就都后移,不相同,更小的指针后移. 以下代码,给出了求对称差集数量的代码. public static int distinctElementCount(int arr1[], int arr2[]) { if (arr1.length == 0) { return arr2.length; } if

Linq 数据操作,两个数组求差、交集、并集

int[] a = { 1, 2, 3, 4, 5, 6, 7 }; int[] b = { 4, 5, 6, 7, 8, 9, 10 }; int[] c = { 1, 2, 3, 3, 4, 1, 2, 4, 6, 1, 6, 5 }; // 交集 var ** = a.Intersect(b); 4,5,6,7 // 并集 var ** = a.Union(b); 1,2,3,4,5,6,7,8,9,10 // a有b没有的 var diff1 = a.Except(b); 1,2,3,9

AutoCAD.Net/C#.Net QQ群:193522571 LinQ 两个集合求交集、并集

//取得交集 Pn = PnFace.Intersect(PnType).ToList(); //取得并集 Pn = PnFace.Concat(PnType).ToList();

【方法】list&lt;?&gt; 两个list集合 查找不同元素,求差值

//方法1 //自己声明list for(int i=0;i<list1.size();i++) { for(int j=0;j<list2.size();j++) { if (list1.get(i).toString().equals(list2.get(j).toString()))//查找相同的元素 { list1.remove(i);//删除list1下标为i的元素 i--; //删除下标后list元素变少需要减少下标值,list不会和数组一样占用空间 break; } } } //

算法学习(3)----求数组中大小最接近的两个元素的差

<算法设计与分析基础>习题1.2 第 9 题如下: 考虑下面这个算法,它求的是数值数组中大小最接近的两个元素的差. 算法: MinDistance(A[0..n-1]) //输入:数字数组 A[0..n-1] //输出:数组中两个大小相差最少的元素的差值 dmin <- ∞ for i <- 0 to n-1 do for j <- 0 to n-1 do if i≠j and |A[[i]-A[j]| < dmin dmin <- |A[i]-A[j]| ret

java求两个集合的差集

public static void main(String[] args) {Set set = new HashSet();Set set1 = new HashSet();set.add("sanny");set.add("mary");set.add("bill");set.add("tom");set.add("tony");set.add("mark");set.add(&q

求两个集合的交集和并集C#

我是用hashset<T>来实现的 具体如代码所示 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace JiaoJi { class Program { static void Main(string[] args) { int [] arrA=new int[8]{1,2,3,4,5,6,7,8}; int [] arrB=new int[5]{4,5,

用顺序表实现求两个集合的并集

#include<iostream.h> #include<malloc.h> #include<limits.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #include<stdlib.h> #include<process.h> #define OK 1 #define INIT_LiST_SIZE 100//顺序表的初始长度 #de