can someone help me with Table.Select(Expression) where expression could be something like this Expression = "id in (1,2,3,4)"

StackOverflow https://stackoverflow.com/questions/20360503

  •  28-08-2022
  •  | 
  •  

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");
有帮助吗?

解决方案

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();
   ......
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top