How do I escape a LIKE clause?

后端 未结 2 2107
情话喂你
情话喂你 2020-12-11 08:15

The code we\'re using is straightforward in this part of the search query:

myCriteria.Add(
    Expression.InsensitiveLike(\"Code\", itemCode, MatchMode.Anywh         


        
2条回答
  •  自闭症患者
    2020-12-11 09:03

    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.

提交回复
热议问题