Syntax Advice
-
foreach (DataRow row in DS.Tables[0])
{
x1.Text = row["test"].ToString();
}shouldn't that be DS.Tables[0].Rows ??
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
-
shouldn't that be DS.Tables[0].Rows ??
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
Yes... I did that the other day too :sigh:
-
Hi, could anyone please help me to upgrade my syntax in c#..
for (int x = 0; x <= DSData.Tables[0].Rows.Count; x++) { x1.Text = DSData.Tables[0].Rows[x]["test"].ToString(); }
dataset DS; foreach (________ in DS) { } ??Dabsukol
Personally I would not do the "upgrade" in the first place as your code will actually run slower. Unless you have a compelling reason to do so rather do the following: int rowCount = DSData.Tables[0].Rows.Count; for (int x = 0; x < rowCount ; x++) { x1.Text = DSData.Tables[0].Rows[x]["test"].ToString(); }
-
Personally I would not do the "upgrade" in the first place as your code will actually run slower. Unless you have a compelling reason to do so rather do the following: int rowCount = DSData.Tables[0].Rows.Count; for (int x = 0; x < rowCount ; x++) { x1.Text = DSData.Tables[0].Rows[x]["test"].ToString(); }
My test says otherwise:
DataTable dt = new DataTable("table1");
DataColumn dc = new DataColumn("col1");
dt.Columns.Add(dc);for (int i = 1; i < 1000000; i++)
{
DataRow row = dt.NewRow();
row["col1"] = "string" + i.ToString();
dt.Rows.Add(row);
}long start = DateTime.Now.Ticks;
for (int i = 0; i < dt.Rows.Count; i++)
{
string s = (string)dt.Rows[i]["col1"];
}long middle = DateTime.Now.Ticks;
foreach (DataRow r in dt.Rows)
{
string s = (string)r["col1"];
}long end = DateTime.Now.Ticks;
TimeSpan forLength = new TimeSpan(middle - start);
TimeSpan foreachLength = new TimeSpan(end - middle);Console.WriteLine("for = " + forLength.ToString());
Console.WriteLine("foreach = " + foreachLength.ToString());Output:
for = 00:00:00.9079668 foreach = 00:00:00.2661282
But in the scheme of things, less than a second to process 1000000 records, either way fine by me.
-
Personally I would not do the "upgrade" in the first place as your code will actually run slower. Unless you have a compelling reason to do so rather do the following: int rowCount = DSData.Tables[0].Rows.Count; for (int x = 0; x < rowCount ; x++) { x1.Text = DSData.Tables[0].Rows[x]["test"].ToString(); }
-
My test says otherwise:
DataTable dt = new DataTable("table1");
DataColumn dc = new DataColumn("col1");
dt.Columns.Add(dc);for (int i = 1; i < 1000000; i++)
{
DataRow row = dt.NewRow();
row["col1"] = "string" + i.ToString();
dt.Rows.Add(row);
}long start = DateTime.Now.Ticks;
for (int i = 0; i < dt.Rows.Count; i++)
{
string s = (string)dt.Rows[i]["col1"];
}long middle = DateTime.Now.Ticks;
foreach (DataRow r in dt.Rows)
{
string s = (string)r["col1"];
}long end = DateTime.Now.Ticks;
TimeSpan forLength = new TimeSpan(middle - start);
TimeSpan foreachLength = new TimeSpan(end - middle);Console.WriteLine("for = " + forLength.ToString());
Console.WriteLine("foreach = " + foreachLength.ToString());Output:
for = 00:00:00.9079668 foreach = 00:00:00.2661282
But in the scheme of things, less than a second to process 1000000 records, either way fine by me.
I stand corrected then :) I was going according to the MSDN Documentation on enumeration overhead. Thanks for the info.
-
My test says otherwise:
DataTable dt = new DataTable("table1");
DataColumn dc = new DataColumn("col1");
dt.Columns.Add(dc);for (int i = 1; i < 1000000; i++)
{
DataRow row = dt.NewRow();
row["col1"] = "string" + i.ToString();
dt.Rows.Add(row);
}long start = DateTime.Now.Ticks;
for (int i = 0; i < dt.Rows.Count; i++)
{
string s = (string)dt.Rows[i]["col1"];
}long middle = DateTime.Now.Ticks;
foreach (DataRow r in dt.Rows)
{
string s = (string)r["col1"];
}long end = DateTime.Now.Ticks;
TimeSpan forLength = new TimeSpan(middle - start);
TimeSpan foreachLength = new TimeSpan(end - middle);Console.WriteLine("for = " + forLength.ToString());
Console.WriteLine("foreach = " + foreachLength.ToString());Output:
for = 00:00:00.9079668 foreach = 00:00:00.2661282
But in the scheme of things, less than a second to process 1000000 records, either way fine by me.
I would suggest you enclose all of that in another for loop that performs 2 or more passes, just to get rid of some effects such as data cache hits. Any first pass measurement typically is unreliable. :)
Luc Pattyn
-
I would suggest you enclose all of that in another for loop that performs 2 or more passes, just to get rid of some effects such as data cache hits. Any first pass measurement typically is unreliable. :)
Luc Pattyn
Good idea: pass 1: for = 00:00:00.0312510 foreach = 00:00:00.0156255 pass 2: for = 00:00:00.0625020 foreach = 00:00:00.0312510 pass 3: for = 00:00:00.0937530 foreach = 00:00:00.0312510 pass 4: for = 00:00:00.1562550 foreach = 00:00:00.0468765
-
Good idea: pass 1: for = 00:00:00.0312510 foreach = 00:00:00.0156255 pass 2: for = 00:00:00.0625020 foreach = 00:00:00.0312510 pass 3: for = 00:00:00.0937530 foreach = 00:00:00.0312510 pass 4: for = 00:00:00.1562550 foreach = 00:00:00.0468765
Well, even these measurements are not trustworthy, your system timer seems to tick in increments of 15.6 msec so that is also the margin of error. I now suggest you redo it in this way:
int maxIter=100; // whatever value makes it take at least 500 msec
for (int pass=0; pass<4; pass++) {
start timer (or read DateTime.Now.Milliseconds)
for (int iter=0; iter<maxIter; iter++) {
code to be timed
}
stop timer
start timer
for (int iter=0; iter<maxIter; iter++) {
alternative code to be timed
}
stop timer
}:)
Luc Pattyn
-
Well, even these measurements are not trustworthy, your system timer seems to tick in increments of 15.6 msec so that is also the margin of error. I now suggest you redo it in this way:
int maxIter=100; // whatever value makes it take at least 500 msec
for (int pass=0; pass<4; pass++) {
start timer (or read DateTime.Now.Milliseconds)
for (int iter=0; iter<maxIter; iter++) {
code to be timed
}
stop timer
start timer
for (int iter=0; iter<maxIter; iter++) {
alternative code to be timed
}
stop timer
}:)
Luc Pattyn
Just for fun I did the test 1000 times and got the following results: for: 00:12:08.2600974 foreach: 00:03:40.6635750 Note that this is total time for 1000 runs; the result seems clear though.
Internet - the worlds biggest dictionary
-
Just for fun I did the test 1000 times and got the following results: for: 00:12:08.2600974 foreach: 00:03:40.6635750 Note that this is total time for 1000 runs; the result seems clear though.
Internet - the worlds biggest dictionary
go with foreach ! :)
Luc Pattyn