Doing something like nested SQL in DataTable.Select method

  • Thread starter Thread starter pplppp
  • Start date Start date
P

pplppp

I have a table with fields the following fields
ID INTEGER (Primary key)
FLAG BOOL
TIMESPAN DATETIME
IMPORTANCE INTEGER

say I want to find the an ID with flag set to true, highest importance
and shortest timespan. so one way to write the SQL is this:

SELECT ID
FROM MyTable
WHERE TIMESPAN =
(SELECT MIN(TIMESPAN )
FROM MyTable
WHERE FLAG=TRUE AND IMPORTANCE =
(SELECT MAX(IMPORTANCE )
FROM MyTable
WHERE FLAG=TRUE));

so. my problem is. the data is in a dataTable and the DataTable.Select
method doesn't take SQL statements. So can something teach me how to
find what I need from the DataTable?

thxs in advance

..Net newB
 
Hi,

If you use plain DataTable you will have to do a loop through all the
records
foreach DataRow dr in table.Rows
Or you might use DataView to filter the records
DataView dv = new DataView(table, "Flag = true", "TimeSpan Asc, Importance
Desc", DataViewRowState.CurrentRows);
That would give you the right record.
 
Back
Top