我如何根据行值在DataTables中查找行 [英] How can i find rows in DataTables according to row value
问题描述
我有一个DataTable,其中可以在列中看到 x:1
x:2
a:1
a:2
etc ...但他们也可以看起来像 x *
或 a *
。
I've got a DataTable in which there could be values in a column which looks like x:1
x:2
a:1
a:2
etc... but they could also look like x*
or a*
.
在我的代码中,我得到一个要搜索的完整值(例如 x:1
),但行本身可以包含一个值,如 x *
在那一列。
我可以用某种方式使用Select方法来搜索该行吗?
In my code I'm getting a full value to search for (for example x:1
), but the row itself can contain a value like x*
in that column.
can i somehow use the Select method to search for the row?
现在看起来像这样:
strSelect = string.Format("[{0}]='{1}'", colName, ValueToSearch);
rows = tempTable.Select(strSelect);
但是,当然,我会得到的唯一的行是那些看起来像一个在桌子里这意味着当搜索 x:1
时,我不会得到 x *
but of course that like that the only rows I'll get are those that look EXACTLY like the one in the table. meaning that when searching for x:1
, i won't get the row with x*
推荐答案
代码 strSelect = string.Format([{0}] ='{1}',colName, ValueToSearch);
将选择相同的值。如果要搜索子集,则必须使用LIKE运算符:
The code strSelect = string.Format("[{0}]='{1}'", colName, ValueToSearch);
will select the same values. If you want search for subset you must use LIKE operator:
strSelect = string.Format("[{0}] LIKE '{1}'", colName, ValueToSearch.Replace("*", "%");
这篇关于我如何根据行值在DataTables中查找行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!