两个 DataTable 读取重复数据,dataTable1与dataTable2不同

 protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("col");
            for (int i = 5; i >0;i=i-2)
            {
                DataRow row = dt.NewRow();
                row["col"] = "车" + i;
                dt.Rows.Add(row);
            }


            DataTable dt1 = new DataTable();
            dt1.Columns.Add("col1");
            for (int i = 1; i <= 4; i++)
            {
                DataRow row = dt1.NewRow();
                row["col1"] = "车" + i;
                dt1.Rows.Add(row);   
            }

            DataTable resultTable = cf(dt,dt1);
            Response.Write("重复:");
            for (int i = 0; i < resultTable.Rows.Count; i++)
                Response.Write(resultTable.Rows[i]["col"].ToString());

            Response.Write("<br/>");


            resultTable = bcfbyd1tod2(dt, dt1);
            Response.Write("不重复 d1tod2:");
            for (int i = 0; i < resultTable.Rows.Count; i++)
                Response.Write(resultTable.Rows[i]["col"].ToString());

            Response.Write("<br/>");


            resultTable = bcfbyd2tod1(dt, dt1);
            Response.Write("不重复 d2tod1:");
            for (int i = 0; i < resultTable.Rows.Count; i++)
                Response.Write(resultTable.Rows[i]["col"].ToString());

            Response.Write("<br/>");

        }

        /// <summary>
        /// 取重复
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="dt1"></param>
        /// <returns></returns>
        private DataTable cf(DataTable dt, DataTable dt1)
        {
            DataTable myTable = dt.Clone();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow[] sRow= dt1.Select("col1='"+dt.Rows[i]["col"].ToString()+"'");

                if (sRow.Length > 0)
                {
                   DataRow row= myTable.NewRow();
                   row["col"] = dt.Rows[i]["col"].ToString();
                   myTable.Rows.Add(row);
                }
            }

            return myTable;
        }


        /// <summary>
        /// 不重复d1tod2
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="dt1"></param>
        /// <returns></returns>
        private DataTable bcfbyd1tod2(DataTable dt, DataTable dt1)
        {
            DataTable myTable = dt.Clone();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow[] sRow = dt1.Select("col1='" + dt.Rows[i]["col"].ToString() + "'");

                if (sRow.Length == 0)
                {
                    DataRow row = myTable.NewRow();
                    row["col"] = dt.Rows[i]["col"].ToString();
                    myTable.Rows.Add(row);
                }
            }

            return myTable;
        }


        /// <summary>
        /// 不重复d1tod2
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="dt1"></param>
        /// <returns></returns>
        private DataTable bcfbyd2tod1(DataTable dt, DataTable dt1)
        {
            DataTable myTable = dt.Clone();

            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                DataRow[] sRow = dt.Select("col='" + dt1.Rows[i]["col1"].ToString() + "'");

                if (sRow.Length == 0)
                {
                    DataRow row = myTable.NewRow();
                    row["col"] = dt1.Rows[i]["col1"].ToString();
                    myTable.Rows.Add(row);
                }
            }

            return myTable;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值