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");
1

There are 1 best solutions below

2
Steve On BEST ANSWER

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();
   ......
}