1. 程式人生 > >c#實現兩個(DataTable)數據合並

c#實現兩個(DataTable)數據合並

eric ima foreach pro pos field tde source pre

在C#代碼中,要將這兩個表拼接起來,也有很多比較“笨”的辦法,就是例如循環獲取數據一條條拼起來,此辦法在數據量少、字段少的可以使用此辦法,但在數據量大的情況下會影響性能,而且字段多的時候也需要寫一大堆給每個字段依次賦值的代碼,性能就有所下降,下列將介紹合並DataTable的幾種方法

主要介紹三種方案

方案一【For循環+RowFilter】

 1             dt_All.Rows.Clear();
 2             dgvResult.DataSource = dt_All;
 3             //方案一:For循環
 4
for (int i = 0; i < dt_Head.Rows.Count; i++) 5 { 6 DataRow drAll = dt_All.NewRow(); 7 drAll["StockID"] = dt_Head.Rows[i]["StockID"]; 8 drAll["ProductName"] = dt_Head.Rows[i]["ProductName"]; 9 DataView dv = new
DataView(dt_Detail); 10 dv.RowFilter = string.Format("StockID = ‘{0}‘", dt_Head.Rows[i]["StockID"]); 11 DataTable dtDetail = dv.ToTable(); 12 if (dtDetail.Rows.Count > 0) 13 { 14 drAll["Num"] = dtDetail.Rows[0]["
Num"]; 15 drAll["Money"] = dtDetail.Rows[0]["Money"]; 16 } 17 dt_All.Rows.Add(drAll); 18 } 19 dgvResult.DataSource = dt_All;

方案二【適合字段比較少的情況下。】

 1             dt_All.Rows.Clear();
 2             dgvResult.DataSource = dt_All;
 3             //方案二:字段少的情況下
 4             var Little = from rHead in dt_Head.AsEnumerable()
 5                          from rDetail in dt_Detail.AsEnumerable()
 6                          where rHead.Field<int>("StockID") == rDetail.Field<int>("StockID")
 7                          select new
 8                          {
 9                              StockID = rHead.Field<int>("StockID"),
10                              ProductName = rHead.Field<string>("ProductName"),
11                              Num = rDetail.Field<int>("Num"),
12                              Money = rDetail.Field<decimal>("Money"),
13                          };
14             DataTable dtNew = dt_All.Copy();
15             foreach (var obj in Little)
16             {
17                 dtNew.Rows.Add(obj.StockID, obj.ProductName, obj.Num, obj.Money);
18             }
19             dgvResult.DataSource = dtNew;

方案三【LINQ提供了與SQL類型的JOIN類似的方法,並且字段比較多的情況下,可使用如下方式。】

 1             dt_All.Rows.Clear();
 2             dgvResult.DataSource = dt_All;
 3             //方案三:字段多的情況下
 4             var query =
 5                 from rHead in dt_Head.AsEnumerable()
 6                 join rDetail in dt_Detail.AsEnumerable()
 7                 on rHead.Field<Int32>("StockID") equals rDetail.Field<Int32>("StockID")
 8                 select rHead.ItemArray.Concat(rDetail.ItemArray.Skip(1));
 9 
10             foreach (var obj in query)
11             {
12                 DataRow dr = dt_All.NewRow();
13                 dr.ItemArray = obj.ToArray();
14                 dt_All.Rows.Add(dr);
15             }
16             dgvResult.DataSource = dt_All;

使用Concat將表1和表2的字段拼接起來,作為總表dt_All的字段,但由於表1、表2中都存在字段StockID,不能在表中出現重復的字段,因此使用Skip(1)跳過表2中的第一個字段StockID。

完整代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class FrmDemo : Form
    {
        DataTable dt_Head = new DataTable();
        DataTable dt_Detail = new DataTable();
        DataTable dt_All = new DataTable();
        public FrmDemo()
        {
            InitializeComponent();
            InitData();
        }

        private void InitData()
        {
            dt_Head.Columns.Add("StockID", typeof(Int32));
            dt_Head.Columns.Add("ProductName", typeof(String));

            dt_Detail.Columns.Add("StockID", typeof(Int32));
            dt_Detail.Columns.Add("Num", typeof(Int32));
            dt_Detail.Columns.Add("Money", typeof(decimal));

            dt_All.Columns.Add("StockID", typeof(Int32));
            dt_All.Columns.Add("ProductName", typeof(String));
            dt_All.Columns.Add("Num", typeof(Int32));
            dt_All.Columns.Add("Money", typeof(decimal));

            for (int i = 1; i < 10; i++)
            {
                DataRow dr_Head = dt_Head.NewRow();
                DataRow dr_Detail = dt_Detail.NewRow();
                dr_Head["StockID"] = i;
                dr_Head["ProductName"] = "商品" + i;

                dr_Detail["StockID"] = i;
                dr_Detail["Num"] = i;
                dr_Detail["Money"] = i * 0.5;

                dt_Head.Rows.Add(dr_Head);
                dt_Detail.Rows.Add(dr_Detail);
            }
        }

        private void btnFor_Click(object sender, EventArgs e)
        {
            dt_All.Rows.Clear();
            dgvResult.DataSource = dt_All;
            //方案一:For循環
            for (int i = 0; i < dt_Head.Rows.Count; i++)
            {
                DataRow drAll = dt_All.NewRow();
                drAll["StockID"] = dt_Head.Rows[i]["StockID"];
                drAll["ProductName"] = dt_Head.Rows[i]["ProductName"];
                DataView dv = new DataView(dt_Detail);
                dv.RowFilter = string.Format("StockID = ‘{0}‘", dt_Head.Rows[i]["StockID"]);
                DataTable dtDetail = dv.ToTable();
                if (dtDetail.Rows.Count > 0)
                {
                    drAll["Num"] = dtDetail.Rows[0]["Num"];
                    drAll["Money"] = dtDetail.Rows[0]["Money"];
                }
                dt_All.Rows.Add(drAll);
            }
            dgvResult.DataSource = dt_All;
        }

        private void btnLittle_Click(object sender, EventArgs e)
        {
            dt_All.Rows.Clear();
            dgvResult.DataSource = dt_All;
            //方案二:字段少的情況下
            var Little = from rHead in dt_Head.AsEnumerable()
                         from rDetail in dt_Detail.AsEnumerable()
                         where rHead.Field<int>("StockID") == rDetail.Field<int>("StockID")
                         select new
                         {
                             StockID = rHead.Field<int>("StockID"),
                             ProductName = rHead.Field<string>("ProductName"),
                             Num = rDetail.Field<int>("Num"),
                             Money = rDetail.Field<decimal>("Money"),
                         };
            DataTable dtNew = dt_All.Copy();
            foreach (var obj in Little)
            {
                dtNew.Rows.Add(obj.StockID, obj.ProductName, obj.Num, obj.Money);
            }
            dgvResult.DataSource = dtNew;
        }

        private void btnMore_Click(object sender, EventArgs e)
        {
            dt_All.Rows.Clear();
            dgvResult.DataSource = dt_All;
            //方案三:字段多的情況下
            var query =
                from rHead in dt_Head.AsEnumerable()
                join rDetail in dt_Detail.AsEnumerable()
                on rHead.Field<Int32>("StockID") equals rDetail.Field<Int32>("StockID")
                select rHead.ItemArray.Concat(rDetail.ItemArray.Skip(1));

            foreach (var obj in query)
            {
                DataRow dr = dt_All.NewRow();
                dr.ItemArray = obj.ToArray();
                dt_All.Rows.Add(dr);
            }
            dgvResult.DataSource = dt_All;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            dt_All.Rows.Clear();
            dgvResult.DataSource = dt_All;
        }
    }
}

c#實現兩個(DataTable)數據合並