0

Is there an example of Table.Select(Expression) where expression could be something like this:

Expression = "id in (1,2,3,4)"

I have a list if ids in a variable IdList. Table contains all the TS_Ids where as the IdList only has subset of the ids. So I want something like this

        string IdList = "(1,2,3,4)";
       Table.Select("[ts_id] in IdList");
8GB
  • 79
  • 3
  • 13

1 Answers1

0

DataTable.Select method uses the DataColumn.Expression property. And the IN operator is listed between the allowed operators in the Expression property. So your code could be written as

DataRow[] rows = Table.Select("[ts_id] in (" + IdList + ")");

Of course this assumes that your IdList variable contains a valid string of Ids separated by commas. something like

IdList = "1,3,5,6";

If you want a DataTable as return value then you could write

DataRow[] rows = Table.Select("[ts_id] in (" + IdList + ")");
if(rows.Length > 0)
{
   DataTable subTable = rows.CopyToDataTable();
   ......
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • I tried this and it is returning me empty table. Table has 3 columns and one of it is `TS_ID`. If I I give it like `DataRow[] rows = Table.Select("[ts_id] = 1");` then it works. But the `"in"` doesnot return anything. – 8GB Dec 03 '13 at 20:08
  • Strange, I have just tested it and a sample returns just the rows requested. Check the parenthesys and the content of your IdList. – Steve Dec 03 '13 at 20:09