C#實現DataTable資料分割處理

violety發表於2024-03-14

【轉自】https://www.cnblogs.com/rongfengliang/p/4385572.html

一個簡單的使用遍歷的方式進行DataTable 的拆分

private static List<DataTable> DataTableSplite(DataTable dt, int modcounts)
        {
            List<DataTable> list = new List<DataTable>();

            int counts = dt.Rows.Count / modcounts;  ///  取整數個數

            int mod = dt.Rows.Count % modcounts;    ///  餘數

            if (mod > 0)
            {
                int index = 0;
                ///  處理拆分倍數的部分
                for (int i = 0; i < counts; i++)
                {

                    DataTable dt1 = dt.Clone();
                    dt1.TableName = "count" + i;
                    for (int j = index * modcounts; j < (index + 1) * modcounts; j++)
                    {

                        DataRow newrow = dt.NewRow();
                        DataRow row1 = dt.Rows[j];
                        newrow["name"] = row1["name"];
                        newrow["age"] = row1["age"];
                        dt1.Rows.Add(newrow.ItemArray);
                    }
                    index++;
                    list.Add(dt1);
                }
                
                ///  處理餘數部分的組合
                DataTable dt2 = dt.Clone();
                dt2.TableName = "modetable";
                for (int i = counts * modcounts; i < dt.Rows.Count; i++)
                {
                    DataRow newrow = dt.NewRow();
                    DataRow row1 = dt.Rows[i];
                    newrow["name"] = row1["name"];
                    newrow["age"] = row1["age"];
                    dt2.Rows.Add(newrow.ItemArray);
                }
                list.Add(dt2);

            }
            else
            {
                ///   餘數為0 的情況處理
                int index = 0;
                for (int i = 0; i < counts; i++)
                {

                    DataTable dt1 = dt.Clone();
                    dt1.TableName = "modetable";
                    for (int j = index * modcounts; j < (index + 1) * modcounts; j++)
                    {

                        DataRow newrow = dt.NewRow();
                        DataRow row1 = dt.Rows[j];
                        newrow["name"] = row1["name"];
                        newrow["age"] = row1["age"];
                        dt1.Rows.Add(newrow.ItemArray);
                    }
                    index++;
                    list.Add(dt1);
                }
            }

            return list;
        }

實現的方式比較簡單,同時也可以使用linq 的方式進行

程式碼如下:

private static List<DataTable> DataTableSplite2(DataTable dt, int modcounts)
        {

            int counts = dt.Rows.Count / modcounts;  ///  取整數個數

            List<DataTable> list = new List<DataTable>();                                     ///  

            int index = 0;
            for (int i = 0; i < counts + 1; i++)
            {
                list.Add(dt.AsEnumerable().Skip(index * modcounts).Take(modcounts).CopyToDataTable());

                index++;
            }
            return list;

        }

public static string PostDataTableData_Page(DataTable dt,string postUrl)
        {

            string rev = "";
            DataTable dtClone = new DataTable();
            int pageSize = 100;
            int count = dt.Rows.Count / pageSize;
for (int i_page = 0; i_page <= count; i_page++) { dtClone = new DataTable(); rev = ""; dtClone = dt.AsEnumerable().Skip(i_page * pageSize).Take(pageSize).CopyToDataTable(); if (dtClone != null && dtClone.Rows.Count > 0) { rev += "["; for (int i = 0; i < dtClone.Rows.Count; i++) { rev += "{"; for (int j = 0; j < dtClone.Columns.Count; j++) { rev += "\"" + dtClone.Columns[j].ColumnName + "\":\"" + dtClone.Rows[i][j].ToString().Replace("<", "&lt;").Replace(">", "&gt;").Replace("'", "&apos;").Replace("\"", "&quot;") + "\","; } rev = rev.TrimEnd(','); rev += "},"; } rev = rev.TrimEnd(','); rev += "]";
//post請求 rev = PostMoths(postUrl, rev); } }

相關文章