Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

foreach Vs. dt.Select

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ben - 06 Mar 2008 18:26 GMT
Hi all,

I need to update an particular set of rows in a datatable.
Here is some sample code of what I want to do:

DataTable dt = (DataTable)Session["Table"];
foreach(DataRow dr in dt.Rows)
{
if(dr.RowState == DataRowState.Added)
//Do something
}

Instead, I could also do:
DataRow[] dr = dt.Select("","",DataViewRowState.Added);
if(dr.Length > 0)
{
for(int i = 0; i < dr.Length; i++)
{
 DataRow drCurrent = dr[i];
 //Do Something
}
}

My question is: Is there a significant performance difference between
the two approaches? The datatable should only have a few records
(maximum 10).

Thanks in advance,
Ben
Peter Duniho - 06 Mar 2008 18:44 GMT
> [...]
> My question is: Is there a significant performance difference between
> the two approaches? The datatable should only have a few records
> (maximum 10).

Without even knowing anything specific about the implementation of  
Select(), I cannot imagine that there's a significant difference between  
the two approaches when the table has only ten rows at most.

That said, this is something that should be easy enough for you to verify  
yourself.  Even without formal profiling tools, just use the Stopwatch  
class to measure the time each implementation takes and compare the two  
directly that way.

By the way, the "if (dr.Length > 0)" is pointless.  The for() loop will do  
that check implicitly, since 0 (the initial value of i) is not less than 0.

Also, foreach() and Select() are not mutually exclusive.  Even if there  
was some performance advantage to using Select(), you could still  
enumerate the results with foreach() rather than using an explicit loop  
variable.  IMHO, using foreach() is preferable when it's an option.

Pete
Ben - 06 Mar 2008 21:32 GMT
Thanks Peter !
Cor Ligthert[MVP] - 12 Mar 2008 07:20 GMT
For sure is the select slower, the select uses an expression which has first
to be translated.

The enumurating through the property rows is for sure as fast as the doing
the same through the collection of rows (although it are probably less
rows). As Peter already wrote, you have to investigate the result of the
select first because it can return null. And then you loose even more time.

We are talking probably about thousands of picoseconds

Cor

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.