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
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();
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()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With