foreach Vs. dt.Select

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben

    foreach Vs. dt.Select

    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)Sess ion["Table"];
    foreach(DataRow dr in dt.Rows)
    {
    if(dr.RowState == DataRowState.Ad ded)
    //Do something
    }

    Instead, I could also do:
    DataRow[] dr = dt.Select("","" ,DataViewRowSta te.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


    *** Sent via Developersdex http://www.developersdex.com ***
  • Cor Ligthert[MVP]

    #2
    Re: foreach Vs. dt.Select

    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

    Comment

    Working...