问题
The problem I have is, my search criteria is:
Row["colName"] != "abc" AND
Row["colName"] != "def" AND
Row["colName"] != "ghic" AND
Row["colName"] != "klm" AND
Row["colName"] != "xyz" AND
Row["colName"] != "mnp" etc..
in other words, after my research I found something about DefaultView
of the DataTable
and RowFilter
, but Rowfilter
seems to filter only by one value.
My situation is I need to filter by a bunch of values.
Thanks
回答1:
You could use Linq-To-DataTable
and a collection of values to exclude.
Query Syntax:
string[] exclude = { "def", "ghic", "klm", "xyz", "mnp" };
var filteredRows = from row in dataTable.AsEnumerable()
where !exclude.Contains(row.Field<string>("colName"))
select row;
DataTable result = filteredRows.CopyToDataTable();
Method Syntax:
result = dataTable.AsEnumerable()
.Where(r => !exclude.Contains(r.Field<string>("colName")))
.CopyToDataTable();
回答2:
You can use AsEnumerable to get an IEnumerable<DataRow>
of the rows, and do a Where
on that.
var criteria = new List<string>();
criteria.Add("abc");
criteria.Add("def");
criteria.Add("ghic");
//etc
var filteredRows = myDataTable.AsEnumerable()
.Where(row => !criteria.Contains(row["colName"].ToString()));
来源:https://stackoverflow.com/questions/27259631/how-to-get-a-subset-datatable-from-another-datatable-filtered-by-few-column-valu