DataTable.Select performance

G

Guest

I am using Visual Studio 2003 and am getting lousy performance after using
a datatable select and then trying to assign a value to a column of the row
that was found:

DataTable dt = new DataTable();
dt.Columns.Add("Number", System.Type.GetType("System.Int32"));
dt.Columns.Add("Value", System.Type.GetType("System.Double"));

for(int i=0;i<2000;i++)
{
DataRow dr = dt.NewRow();
dr["Number"] = i;
dr["Value"] = 1.0;
dt.Rows.Add(dr);
}

for(int i=0;i<2000;i++)
{
// use the select
string filter = "Number = " + i;
DataRow[] rows = dt.Select(filter);

// assign a new value to the row that was found
if(rows.Length > 0) rows[0]["Value"] = 2;
}

I want to note that this is only if both a select and assign are used
together. Performance is fine if one is used without the other. Anyone
encounter this and know of a workaround?
 
J

Jay B. Harlow [MVP - Outlook]

Luc,
I want to note that this is only if both a select and assign are used
together. Performance is fine if one is used without the other. Anyone
encounter this and know of a workaround?

I would expect updating a value in a DataTable to take a certain amount of
time. As the previous values need to be saved & the new values set. Remember
that a DataRow actually contains upto 4 versions of the row based on the
DataRowVersion enum.

Interesting enough, if you define a primary key on the Number column (the
column you are selecting on) then the select & update goes about as fast as
just Select. As defining an "index" (Primary Key or DataView) on a column
will make Select go faster, as Select will use this "index".
dt.Columns.Add("Number", System.Type.GetType("System.Int32"));

dt.PrimaryKey = new DataColumn[] {dt.Columns[Number]}

Alternatively you can use a DataView & FindRows if the column you are
selecting on is not the primary key column.

Hope this helps
Jay

Luc said:
I am using Visual Studio 2003 and am getting lousy performance after using
a datatable select and then trying to assign a value to a column of the
row
that was found:

DataTable dt = new DataTable();
dt.Columns.Add("Number", System.Type.GetType("System.Int32"));
dt.Columns.Add("Value", System.Type.GetType("System.Double"));

for(int i=0;i<2000;i++)
{
DataRow dr = dt.NewRow();
dr["Number"] = i;
dr["Value"] = 1.0;
dt.Rows.Add(dr);
}

for(int i=0;i<2000;i++)
{
// use the select
string filter = "Number = " + i;
DataRow[] rows = dt.Select(filter);

// assign a new value to the row that was found
if(rows.Length > 0) rows[0]["Value"] = 2;
}

I want to note that this is only if both a select and assign are used
together. Performance is fine if one is used without the other. Anyone
encounter this and know of a workaround?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top