Well, a
filterExpression
parameter of
DataTable.Select method[
^] creates a
DataColumn.Expression[
^].
There's a list of applicable functions, operators, which can be used to define expression.
IN
is also available and you have to pass values as a comma separated string. Take a look at example:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("project_type", typeof(int)));
dt.Columns.Add(new DataColumn("tc_id", typeof(int)));
Random r = new Random();
dt = Enumerable.Range(1, 50)
.Select(x=> dt.LoadDataRow(new object[]{r.Next(x)+1,r.Next(x+1)+1}, false))
.CopyToDataTable();
string tcs = "1,2,3";
string pts = "5,8,10";
var filtered = dt.Select(string.Format("project_type IN({0}) OR tc_id IN({1})", pts, tcs));
Above code returns, for example:
project_type tc_id
1 1
2 2
3 2
3 2
3 3
4 2
4 2
5 1
5 2
5 16
6 3
8 9
8 21
10 15
10 18
10 34
15 1
26 2
The way a datatable has been filled is not important, but - as you can see -
filterExpression
works fine ;)
There's at least few ways to filter data:
1. On server (database) level (via SQL query), - strongly recommended!
2. On client level, through filetering datatable, views, etc., using:
- DataColumn expressions (
Select
method)
-
LINQ to DataSet | Microsoft Docs[
^]
Good luck!