Question on DataTable.Select()

G

Guest

Hello All,

I am trying to filter rows in a datatable based on filtercriteria and
sortcriteria using the datatable.select() method.

I am encountering a strange behavior in this process. Here is what I am
trying to do...

---------

1. Queried a table based on a filtercriteria and retrieved 40 rows into a
table in a dataset. Now my dataset's 0th table has 40 rows.

2. Now trying to select rows in the 0th table of the above dataset using a
filtercriteria.

Technically speaking if I run a select method over a parent table, resulting
child table should have less number of rows than parent table ....right? How
come my step 2 is returning 43 rows?

All I can conclude is select method is filtering the rows from the original
database table and not on the step 1 dataset's 0th table. I am sure that I am
using the correct syntax....

---

int noRowsParentTable = objDataSet.Tables[0].Rows.Count;

foundRows = objDataSet.Tables[0].Select("ID < 4600");

int noRowsChildTable = foundRows.Length;

----

noRowsParentTable = 211 rows
noRowsChildTable = 1327 rows

Can anyone please correct me? Or explain the strange behavior?

TIA,
Diffi
 
K

Karl Seguin

This behaviour is not normal. I tested with a sample and everything worked
like we would expect...

Here's my complete code:


SqlConnection connection = new SqlConnection(CONNECTION_STRING);
SqlCommand command = new SqlCommand("SELECT * FROM Relevance", connection);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(command);
try
{
da.Fill(ds);
}finally
{
connection.Dispose();
command.Dispose();
da.Dispose();
}
Response.Write(ds.Tables[0].Rows.Count);
Response.Write("-");
DataRow[] rows = ds.Tables[0].Select("RelevanceID < 4");
Response.Write(rows.Length);


Relevance table is just an ID and a Key

first one outputted 53, 2nd one outputted 4


Absolutely no clue on what's wrong with yours..are the objects (dataset,
datarow array) scopped locally? are they global and/or static..that's all I
could think of as potential culprits...

You should use a DataView and the Filter property for this stuff, though the
behaviour is still odd..

DataView dv = ds.Tables[0].DefaultView;
dv.Filter = "ID < 4";

and you'll see the filtered data from the original datatable (much like an
SQL view works)...

Karl
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top