Data transpose of datatable

Source: Internet
Author: User

No specific tests have been conducted. The 1.3 million pieces of data may be ~ About 2 seconds, but I personally feel that it is troublesome to process database paging or write conditions. It is better to use database paging!

However, this method is a way of thinking.

 

Private void form1_load (Object sender, eventargs E)
{
# Region
Datatable dt = new datatable ();
DT. Columns. Add ("name ");
DT. Columns. Add ("name2 ");
DT. Columns. Add ("type ");
DT. Columns. Add ("count ");

Random ran3 = new random ();
Random ran = new random ();
Random ran2 = new random ();

Stopwatch stopwatch = new stopwatch ();
Stopwatch. Start ();
Datarow Dr;
For (INT I = 0; I <50000; I ++)
{
Int K3 = ran3.next (1, 2000 );
Int K = ran. Next (1, 13 );
Int c = rand2.next (50,100 );

Dr = DT. newrow ();

Dr ["name"] = "N" + k3.tostring ();
Dr ["name2"] = "N2" + k3.tostring ();
Dr ["type"] = "T" + K. tostring ();
Dr ["count"] = "CO" + C. tostring ();
DT. Rows. Add (DR );
}
Stopwatch. Stop ();
Console. writeline (stopwatch. elapsedmilliseconds. tostring ());

# Endregion
Stopwatch. Restart ();
Datatable dt2 = getdt (DT, "type", "Count", "name", "name2 ");
Stopwatch. Stop ();
Console. writeline (stopwatch. elapsedmilliseconds. tostring ());
This. Maid = dt2;
}
/// <Summary>
///
/// </Summary>
/// <Param name = "dtsource"> data source </param>
/// <Param name = "colname"> columns to be transposed into column names, such as the city column </param>
/// <Param name = "dataname"> data columns to be transposed, such as the Count column </param>
/// <Param name = "Val"> other distinguished columns, such as the name2 column </param>
/// <Returns> </returns>
Datatable getdt (datatable dtsource, string colname, string dataname, Params string [] fileds)
{
Datatable dt = new datatable ();

Datatable newtable = dtsource. defaultview. totable (true, fileds );

Datatable dtcol = dtsource. defaultview. totable (true, colname );

Foreach (datarow item in dtcol. Rows)
{
Newtable. Columns. Add (item [0]. tostring ());
}

For (INT I = 0; I <newtable. Rows. Count; I ++)
{
String strsel1 = "1 = 1 ";
For (int K = 0; k <fileds. length; k ++) // process data starting
{
String rowname = newtable. Rows [I] [K]. tostring ();
Newtable. Rows [I] [k] = rowname;
Strsel1 + = string. Format ("and {0} = '{1}'", fileds [K], rowname );
}

For (Int J = fileds. length; j <newtable. Columns. Count; j ++)
{
String cname = newtable. Columns [J]. columnname;

String strsel2 = string. Format ("{0} = '{1}'", colname, cname );

Datarow [] DN = dtsource. Select (strsel1 + "and" + strsel2 );

If (DN! = NULL & DN. length> 0)
{
Newtable. Rows [I] [J] = DN [0] [dataname]. tostring ();
}
}
}

Return newtable;
}

 

 

 

Data transpose of datatable

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.