您收到错误是因为参数Select
是filterExpression并且您已经传递了所有列。将filterExpression理解为WHERE
sql中的子句。您想要所有列,但只想按一列进行过滤。无论如何,您都会获得所有列,因为它们都是DataTable
/的一部分,DataView
因此您无需明确列出它们。
您可以使用DataTable.Select
,DatView.RowFilter
方法或LINQ-to-DataSet
:
LINQ-To-DataSet(我更喜欢):
var filtered = tb.AsEnumerable()
.Where(r => r.Field<String>("CREATOR").Contains(searchstring));
ADO.NET(DataTable.Select
):
DataRow[] filteredRows = tb.Select("CREATOR LIKE '%" + searchstring + "%'");
ADO.NET(DataView.RowFilter
):
tb.DefaultView.RowFilter = "CREATOR LIKE '%" + searchstring + "%'";
string
如果您想在任何列中搜索此内容:
DataRow[] filteredRows = tb.Select("FIRSTNAME LIKE '%" + searchstring + "%' OR LASTNAME LIKE '%" + searchstring + "%' OR NAME LIKE '%" + searchstring + "%' OR COMPANY LIKE '%" + searchstring + "%' OR CREATOR LIKE '%" + searchstring + "%'");
与 Linq 相同:
var filtered = tb.AsEnumerable()
.Where(r => r.Field<String>("FIRSTNAME").Contains(searchstring)
|| r.Field<String>("LASTNAME").Contains(searchstring))
|| r.Field<String>("NAME").Contains(searchstring)
|| r.Field<String>("COMPANY").Contains(searchstring)
|| r.Field<String>("CREATOR").Contains(searchstring));