Skip to content

Commit

Permalink
Call ToString method only if bodyType is not string
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezanet committed Aug 4, 2021
1 parent 72e3807 commit ca492ca
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/Core/SyntaxTree/SyntaxTreeToQueryConvertor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,49 @@ private static Expression<Func<T, bool>> ConvertBinaryExpressionSyntaxToQuery<T>
be = Expression.Not(Expression.Call(body, GetContainsMethod(), Expression.Constant(value, body.Type)));
break;
case SyntaxKind.StartsWith:
body = Expression.Call(body, GetToStringMethod());
be = Expression.Call(body, GetStartWithMethod(), Expression.Constant(value?.ToString(), body.Type));
break;
if (body.Type != typeof(string))
{
body = Expression.Call(body, GetToStringMethod());
be = Expression.Call(body, GetStartWithMethod(), Expression.Constant(value?.ToString(), body.Type));
}
else
be = Expression.Call(body, GetStartWithMethod(), Expression.Constant(value, body.Type));

break;
case SyntaxKind.EndsWith:
body = Expression.Call(body, GetToStringMethod());
be = Expression.Call(body, GetEndsWithMethod(), Expression.Constant(value?.ToString(), body.Type));
if (body.Type != typeof(string))
{
body = Expression.Call(body, GetToStringMethod());
be = Expression.Call(body, GetEndsWithMethod(), Expression.Constant(value?.ToString(), body.Type));
}
else
be = Expression.Call(body, GetEndsWithMethod(), Expression.Constant(value, body.Type));

break;
case SyntaxKind.NotStartsWith:
body = Expression.Call(body, GetToStringMethod());
be = Expression.Not(Expression.Call(body, GetStartWithMethod(), Expression.Constant(value?.ToString(), body.Type)));
if (body.Type != typeof(string))
{
body = Expression.Call(body, GetToStringMethod());
be = Expression.Not(Expression.Call(body, GetStartWithMethod(), Expression.Constant(value?.ToString(), body.Type)));
}
else
be = Expression.Not(Expression.Call(body, GetStartWithMethod(), Expression.Constant(value, body.Type)));

break;
case SyntaxKind.NotEndsWith:
body = Expression.Call(body, GetToStringMethod());
be = Expression.Not(Expression.Call(body, GetEndsWithMethod(), Expression.Constant(value?.ToString(), body.Type)));
if (body.Type != typeof(string))
{
body = Expression.Call(body, GetToStringMethod());
be = Expression.Not(Expression.Call(body, GetEndsWithMethod(), Expression.Constant(value?.ToString(), body.Type)));
}
else
be = Expression.Not(Expression.Call(body, GetEndsWithMethod(), Expression.Constant(value, body.Type)));

break;
default:
return null;
}

return Expression.Lambda<Func<T, bool>>(be, exp.Parameters);
}
catch (Exception)
Expand All @@ -109,7 +134,7 @@ private static Expression<Func<T, bool>> ConvertBinaryExpressionSyntaxToQuery<T>
private static MethodInfo GetStartWithMethod() => typeof(string).GetMethod("StartsWith", new[] {typeof(string)});

private static MethodInfo GetContainsMethod() => typeof(string).GetMethod("Contains", new[] {typeof(string)});

private static MethodInfo GetToStringMethod() => typeof(object).GetMethod("ToString");

internal static Expression<Func<T, bool>> GenerateQuery<T>(ExpressionSyntax expression, IGridifyMapper<T> mapper)
Expand Down

0 comments on commit ca492ca

Please sign in to comment.