Choosing a DataTable: Expression with a Space

Situation:

Hello! I have a little problem in a C # project. I use the Select method from the DataTable and use the expression to get what I want, but I'm having problems with space in one of the lines that I use for the expression.

So here is an example of the code that I have:

DataTable table;
//...  
DataRow[] rows = table.Select("[" + columnNameStr + "]" + " LIKE '*" + searchStr + "*'");
//...

The searchStr string may contain a space. So, for example, if you have a name such as Bob Dude in searchStr , and the string should be Dude Bob, and the select expression will not return any result.


Question:

What expression do I need to use if I want to get the result when the words in searchStr are not necessarily in the correct order to get the result?

+3
2

, , OR. - :

var searchStr = "Bob Dude";
var splitSearchString = searchStr.Split(' ');
var columnNameStr = "Name";
var expression = new List<string>();
DataTable table = new DataTable();
//...  
foreach (var searchElement in splitSearchString)
{
    expression.Add(
        string.Format("[{0}] LIKE '*{1}*'", columnNameStr, searchElement));
}
var searchExpressionString = string.Join(" OR ", expression.ToArray());
DataRow[] rows = table.Select(searchExpressionString);
+2

.

, searchStr "Hello World".

DataTable.Select("ColumnName" + "'" + searchStr + "'");

(') .

+3

Source: https://habr.com/ru/post/1715285/


All Articles