private static Expression BuildRightExpression (Expression unescapedSearchText, string likePrefix, string likePostfix)
{
Expression rightExpression;
var argumentAsConstantExpression = unescapedSearchText as ConstantExpression;
if (argumentAsConstantExpression != null)
{
if (argumentAsConstantExpression.Value == null)
rightExpression = null;
else
{
var escapedSearchText = SqlLikeExpression.Escape ((string) argumentAsConstantExpression.Value, @"\");
rightExpression = Expression.Constant (string.Format ("{0}{1}{2}", likePrefix, escapedSearchText, likePostfix));
}
}
else
{
var escapedSearchExpression = SqlLikeExpression.Escape (unescapedSearchText, @"\");
var concatMethod = typeof (string).GetMethod ("Concat", new[] { typeof (string), typeof (string) });
var patternWithPrefix =
!string.IsNullOrEmpty (likePrefix)
? Expression.Add (new SqlLiteralExpression (likePrefix), escapedSearchExpression, concatMethod)
: escapedSearchExpression;
var patternWithPrefixAndPostfix =
!string.IsNullOrEmpty (likePostfix)
? Expression.Add (patternWithPrefix, new SqlLiteralExpression (likePostfix), concatMethod)
: patternWithPrefix;
rightExpression = patternWithPrefixAndPostfix;
}
return rightExpression;
}