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