The datatable in. Net is a very good container for storing data, just like a mobile database.
Sometimes, datatable stores a large amount of data, but not all of the data is useful for you. In this case, you need to quickly extract the data you need. The following methods are used to extract data from datatable:
1. Use the for loop statement; 2. Use the foreach loop statement; 3. Use the select () method in the datatable class.
First, let's look at a piece of code:
Datatable dt = new datatable ();
DT. Columns. Add ("I _id", typeof (string ));
Datetime dt1 = datetime. now;
For (INT I = 0; I <1000000; I ++)
{
DT. Rows. Add (Dt. newrow ());
DT. Rows [I] ["I _id"] = I. tostring ();
}
Timespan sp = datetime. Now-dt1;
String SP1 = sp. totalmilliseconds. tostring ();
Datetime dt2 = datetime. now;
Datarow [] drs1 = DT. Select ("I _id = '2013 '");
SP = datetime. Now-dt2;
String SP2 = sp. totalmilliseconds. tostring ();
Datetime dt3 = datetime. now;
Datarow [] drs2 = DT. Select ("I _id = '2013'", "I _id DESC ");
SP = datetime. Now-dt3;
String SP3 = sp. totalmilliseconds. tostring ();
Datetime dt4 = datetime. now;
Datarow [] drs3 = DT. Select ("I _id = '2013'", "I _id DESC", dataviewrowstate. currentrows );
SP = datetime. Now-dt4;
String SP4 = sp. totalmilliseconds. tostring ();
Datetime dt5 = datetime. now;
Datarow [] drs4 = DT. Select ("I _id = '2013'", "I _id ASC ");
SP = datetime. Now-dt5;
String SP5 = sp. totalmilliseconds. tostring ();
Datetime dt6 = datetime. now;
For (INT I = 0; I <1000000; I ++)
{
If (Dt. Rows [I] ["I _id"]. Equals ("999999 "))
{
Break;
}
}
SP = datetime. Now-dt6;
String SP6 = sp. totalmilliseconds. tostring ();
Datetime dt7 = datetime. now;
Foreach (datarow DR in DT. Rows)
{
If (Dr ["I _id"]. Equals ("999999 "))
{
Break;
}
}
SP = datetime. Now-dt7;
String sp7 = sp. totalmilliseconds. tostring ();
Response. write (SP1 + "& nbsp;" + SP2 + "& nbsp;" + SP3 + "& nbsp;" + SP4 + "& nbsp;" + SP5 + "& nbsp; "+ SP6 +" & nbsp; "+ sp7 );
Then let's look at the result:
13943.3133 17093.2977 0 0 1621.3155 386.0275
Through the displayed results, we should understand which method is the most efficient.