利用DateSet实现多个数组的快速比较(.net)

                                                                                       
新建1个
DataSet ,并添加列a,b
b列用来识别来源
把列的Unique属性设为true

当把数组1添到table,b=1
再把数组2添到table,   b=2
由于完整性
数组2添加以存在的项时会报异常
添加完之后
b=2的就是数组2有数组1没有的项

             

1                      2

  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using System.Data;
  5using System.Collections;
  6namespace WindowsApplication1
  7{
  8    class Ccmp
  9    {
 10        
 11        System.Data.DataSet ds=new DataSet();
 12        public System.Data.DataTable tb;
 13        ArrayList A = new ArrayList();
 14        ArrayList B = new ArrayList();
 15        public Ccmp()
 16        {
 17            tb=ds.Tables.Add("cmptb");
 18            tb.Columns.Add("content");
 19            tb.Columns["content"].Unique = true;
 20            tb.Columns.Add("class");
 21            tb.Columns.Add("id");
 22            tb.Columns["id"].AutoIncrement = true;
 23            /* content class id  */
 24            
 25        }

 26        void Tb_Fill_ADD()
 27        {
 28            tb.Clear();
 29
 30            for (int i = 0; i < A.Count; i++)
 31            {
 32                tb.Rows.Add(A[i],"1");
 33            }

 34            for (int i = 0; i < B.Count; i++)
 35            {
 36                try
 37                {
 38                    tb.Rows.Add(B[i], "2");
 39                }

 40                catch (Exception)
 41                continue; }
 42            }

 43        
 44        }

 45        void Tb_Fill_DEL()
 46        {
 47            tb.Clear();
 48
 49            for (int i = 0; i < B.Count; i++)
 50            {
 51                tb.Rows.Add(B[i], "1");
 52            }

 53            for (int i = 0; i < A.Count; i++)
 54            {
 55                try
 56                {
 57                    tb.Rows.Add(A[i], "2");
 58                }

 59                catch (Exception)
 60                continue; }
 61            }

 62
 63        }

 64        public ArrayList Cmp_Add()/// 返回B相对A添加了的
 65        {
 66            Tb_Fill_ADD();
 67            ArrayList temp = new ArrayList();
 68            for (int i = 0; i < tb.Rows.Count; i++)
 69            {
 70                if (tb.Rows[i]["class"].ToString()=="2")
 71               temp.Add( tb.Rows[i]["content"]);
 72            }

 73
 74            return temp;
 75        }

 76        public ArrayList Cmp_Del()///返回B相对A删除了的
 77        {
 78            Tb_Fill_DEL();
 79            ArrayList temp = new ArrayList();
 80            for (int i = 0; i < tb.Rows.Count; i++)
 81            {
 82                if (tb.Rows[i]["class"].ToString() == "2")
 83                    temp.Add(tb.Rows[i]["content"]);
 84            }

 85
 86            return temp;
 87        }

 88
 89        public void Clear_AB()
 90        {
 91            A.Clear();
 92            B.Clear();
 93        }

 94        public void Add_A(object content)
 95        {
 96            A.Add(content);
 97        }

 98        public void Add_B(object content)
 99        {
100            B.Add(content);
101        }

102
103    }

104}

105
106

 

 

原文地址:https://www.cnblogs.com/ahuo/p/693931.html