The code we\'re using is straightforward in this part of the search query:
myCriteria.Add(
Expression.InsensitiveLike(\"Code\", itemCode, MatchMode.Anywh
The only way I can find to achieve database independence is to escape every character in the search string and call the appropriate constructor on LikeExpression as in my previous answer. You could do this manually or extend LikeExpression:
public class LikeExpressionEscaped : LikeExpression
{
private static string EscapeValue(string value)
{
var chars = value.ToCharArray();
var strs = chars.Select(x => x.ToString()).ToArray();
return "\\" + string.Join("\\", strs);
}
public LikeExpressionEscaped(string propertyName, string value, MatchMode matchMode, bool ignoreCase)
: base(propertyName, EscapeValue(value), matchMode, '\\', ignoreCase)
{}
}
There is no doubt a more efficient way to create the escaped string (see the answers and comments to this question). Usage is:
var exp = new LikeExpressionEscaped("Code", itemCode, MatchMode.Anywhere, true);
myCriteria.Add(exp);
You can create an instance of LikeExpression to accomplish this. In this example I am escaping % with a backslash (which itself has to be escaped):
var itemCode = "ItemWith%Symbol";
itemCode = searchCode.Replace("%", "\\%");
var exp = new LikeExpression("Code", itemCode, MatchMode.Anywhere, '\\', true);
myCriteria.Add(exp);
I didn't see a static method to return a LikeExpression using this overload.
By the way, if you're using SQL Server it is case insensitive by default.