问题
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*
.
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?
for now it looks something like this:
strSelect = string.Format("[{0}]='{1}'", colName, ValueToSearch);
rows = tempTable.Select(strSelect);
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*
回答1:
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("*", "%");
回答2:
I'm assuming for the moment that your database includes 4 rows, with the following values in a given column that you're wanting to query against:
- x:1
- x:2
- x*
- a:1
- a:2
- a*
You state that you're being handed a value such as 'x:1' which you need to use in your query, but you're implying that the query should end up return the first three records - those with values of 'x:1', 'x:2', and 'x*'. In other words, although you're being handed 'x:1', you're actually want to search for any records that have a value that begins with 'x'.
If that's the scenario, you're probably best off modifying the value in your C# code before issuing the query. If your search value is genuinely of the form 'x:1', you could just chop off the last two characters before handing it to the SQL query:
string searchValue = "x:1"; // this presumably actually comes from user input
searchValue = searchValue.Substring(0, searchValue.Length - 2);
// Now searchValue is just "x", so go ahead and create your SQL query using the 'LIKE' operator
I have the feeling this is just a simplification of your actual data though, which makes it hard to be precise & also makes it harder to provide an example that includes error-checking.
For a slightly more complex example, perhaps the search-value your user gives you can either be a string of letters, or a string of letters followed by a colon followed by more letters. In that case, you need to check whether the string you've been given contains a colon, and if it does you need to chop off the colon and anything following it:
string searchValue = "abc:def";
if (searchValue.Contains(":"))
searchValue = searchValue.Substring(0, searchValue.IndexOf(":"));
// Having stripped off ":def", you're left with "abc"
Now you can go ahead and issue a query, using the LIKE operator, as TcKs already showed in his answer. For example you could modify the query code you already have as follows:
strSelect = string.Format("[{0}] LIKE '{1}'", colName, ValueToSearch);
rows = tempTable.Select(strSelect);
By using the LIKE operator, you're now looking for any records that have a value which starts with "abc".
来源:https://stackoverflow.com/questions/11757599/how-can-i-find-rows-in-datatables-according-to-row-value