Find a value in DataTable
Is there a way to find a value in DataTable in C# without doing row-by-row operation?
The value can be a part of (a substring of row[columnName].value , separated by comma) a cell in the datatable and the value may be present in any one of columns in the row.
A DataTable or DataSet object will have a Select Method that will return a DataRow array of results based on the query passed in as it's parameter.
Looking at your requirement your filterexpression will have to be somewhat general to make this work.
myDataTable.Select("columnName1 like '%" + value + "%'");
也许你可以通过这样的可能的列来过滤行:
DataRow[] filteredRows =
datatable.Select(string.Format("{0} LIKE '%{1}%'", columnName, value));
AFAIK, there is nothing built in for searching all columns. You can use Find
only against the primary key. Select
needs specified columns. You can perhaps use LINQ, but ultimately this just does the same looping. Perhaps just unroll it yourself? It'll be readable, at least.
上一篇: 检查日期是否在SQL中重叠
下一篇: 在DataTable中查找值