private Expression MapStringLikeExpression(MethodCallExpression node, bool matchStart, bool matchEnd) { var expression = base.VisitMethodCall(node) as MethodCallExpression; if ((expression.Arguments == null) || (expression.Arguments.Count != 1)) { throw new ApplicationException("Did not find exactly 1 Argument to StartsWith function"); } DbExpression srcExpression = GetDbExpressionForExpression(expression.Object); DbExpression dbExpression; if (expression.Arguments[0] is ConstantExpression) { var constantExpression = GetDbExpressionForExpression(expression.Arguments[0]) as DbConstantExpression; if ((constantExpression == null) || (constantExpression.Value == null)) { throw new NullReferenceException("Parameter to StartsWith cannot be null"); } string value = matchStart ? "" : "%"; value += constantExpression.Value.ToString(); if (!matchEnd) { value += "%"; } dbExpression = DbExpressionBuilder.Like(srcExpression, DbExpressionBuilder.Constant(value)); } else { var argExpression = GetDbExpressionForExpression(expression.Arguments[0]); // Note: Can also do this using StartsWith function on srcExpression (which avoids having to hardcode the % character). // It works but generates some crazy conditions using charindex which I don't think will use indexes as well as "like"... //dbExpression = DbExpressionBuilder.Equal(DbExpressionBuilder.True, srcExpression.StartsWith(argExpression)); DbExpression value = matchStart ? argExpression : DbExpressionBuilder.Constant("%").Concat(argExpression); if (!matchEnd) { value = value.Concat(DbExpressionBuilder.Constant("%")); } dbExpression = DbExpressionBuilder.Like(srcExpression, value); } MapExpressionToDbExpression(expression, dbExpression); return(expression); }