General information
Title:
Getting unrelated rows from a DataTable
Environment versions
Network:
Windows 2003 Server
Hi,
How can I get a subset of rows from a DataTable when all I have is a collection of primary keys? The rows that I want from the DataTable have no common data/keys so I unfortunately can't just use DataTable.Select("someColumn = @someValue"). From an SQL perspective the question be : I have a fistfull of primary key values and I want to retrieve the rows that have those primary key values)
The two ways that I have thought of and partly rejected are :
1. use DataTable.Select(string filter) and pass in an OR filter string that is built up of the primary keys that I want rows for. For example :
DataTable.Select["pk = @pk1 OR pk = @pk2 OR pk = @pk3 OR ...").
This seems like a brute force solution that is open to all kinds of problems and I would like to avoid it if I could.
2. Iterate through the collection of PKs and build up a collection of the rows that I want. For example :
RowCollection rowsThatIWant = new RowCollection();
foreach(object PK in PKCollection)
{
DataRow foundRow = DataTable.Rows.Find(PK);
rowsThatIWant.Add(foundRow);
}
I am afraid that this solution could take a long time if the collection of PKs is big (execution time is an important feature for our application)
Any better solutions would be greatly appreciated,
Andrew
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only