C # Universal DataTable split Small table

Source: Internet
Author: User

A simple way to use traversal to split a DataTable

 Private StaticList<datatable> datatablesplite (DataTable DT,intmodcounts) {List<DataTable> list =NewList<datatable>(); intcounts = dt. rows.count/modcounts;///take an integer number of numbers            intMoD = dt. Rows.Count% Modcounts;///remainder            if(MoD >0)            {                intindex =0; ///the part that handles the split multiplier                 for(inti =0; I < counts; i++) {DataTable dt1=dt.                    Clone (); Dt1. TableName="Count"+i;  for(intj = 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); }                                ///the combination of the processing remainder partDataTable DT2 =dt.                Clone (); DT2. TableName="modetable";  for(inti = 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            {                ///case with a remainder of 0                intindex =0;  for(inti =0; I < counts; i++) {DataTable dt1=dt.                    Clone (); Dt1. TableName="modetable";  for(intj = 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); }            }            returnlist; }

is easier to implement, and can be done in a way that uses LINQ

The code is as follows:

Private StaticList<datatable> DataTableSplite2 (DataTable DT,intmodcounts) {            intcounts = dt. rows.count/modcounts;///take an integer number of numberslist<datatable> list =NewList<datatable> ();///              intindex =0;  for(inti =0; I < counts +1; i++) {list. ADD (dt. AsEnumerable (). Skip (Index*modcounts). Take (modcounts).                CopyToDataTable ()); Index++; }            returnlist; }

Less code.

C # Universal DataTable split Small table

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.