Skip to content

Commit

Permalink
refactor: LinqQueryBuilder.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezanet committed Jun 22, 2024
1 parent a5c5137 commit a5a7077
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Gridify/QueryBuilders/LinqQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class LinqQueryBuilder<T>(IGridifyMapper<T> mapper) : BaseQueryBuilder<

if (conditionExp is not LambdaExpression lambdaExp) return null;

return LinqQueryBuilder<T>.ParseMethodCallExpression(selectExp, lambdaExp, op) as Expression<Func<T, bool>>;
return ParseMethodCallExpression(selectExp, lambdaExp, op) as Expression<Func<T, bool>>;
}
case ConditionalExpression cExp:
{
Expand Down Expand Up @@ -269,7 +269,7 @@ protected override Expression<Func<T, bool>> CombineWithOrOperator(Expression<Fu

if (conditionExp is not LambdaExpression lambdaExp) return null;

return LinqQueryBuilder<T>.ParseMethodCallExpression(selectExp, lambdaExp, op) as Expression<Func<T, bool>>;
return ParseMethodCallExpression(selectExp, lambdaExp, op) as Expression<Func<T, bool>>;
}
case ConditionalExpression cExp:
{
Expand Down Expand Up @@ -316,7 +316,7 @@ when subExp.Arguments.Last()
is LambdaExpression { Body: MemberExpression lambdaMember }:
{
var newPredicate = GetAnyExpression(lambdaMember, predicate);
return LinqQueryBuilder<T>.ParseMethodCallExpression(subExp, newPredicate, op);
return ParseMethodCallExpression(subExp, newPredicate, op);
}
case MethodCallExpression { Method.Name: "Select" } subExp
when subExp.Arguments.Last() is LambdaExpression
Expand All @@ -326,7 +326,7 @@ when subExp.Arguments.Last() is LambdaExpression
{
var newExp = new ReplaceExpressionVisitor(predicate.Parameters[0], lambdaMember).Visit(predicate.Body);
var newPredicate = GetExpressionWithNullCheck(lambdaMember, lambda.Parameters[0], newExp);
return LinqQueryBuilder<T>.ParseMethodCallExpression(subExp, newPredicate, op);
return ParseMethodCallExpression(subExp, newPredicate, op);
}
default:
throw new InvalidOperationException();
Expand All @@ -343,7 +343,7 @@ private static LambdaExpression GetContainsExpression(MemberExpression member, B
: prop.Type.GetElementType(); // array

if (tp == null) throw new GridifyFilteringException($"Can not detect the '{member.Member.Name}' property type.");
var containsMethod = typeof(Enumerable).GetMethods().First(x => x.Name == "Contains").MakeGenericMethod(tp);
var containsMethod = GetContainsMethod(tp);
Expression containsExp = Expression.Call(containsMethod, prop, binaryExpression.Right);
if (op.Kind == SyntaxKind.NotEqual)
{
Expand All @@ -352,6 +352,8 @@ private static LambdaExpression GetContainsExpression(MemberExpression member, B
return GetExpressionWithNullCheck(prop, param, containsExp);
}



private static ParameterExpression GetParameterExpression(MemberExpression member)
{
return member.Expression switch
Expand Down Expand Up @@ -484,6 +486,11 @@ private static MethodInfo GetStringContainsMethod()
return typeof(string).GetMethod("Contains", new[] { typeof(string) })!;
}

private static MethodInfo GetContainsMethod(Type tp)
{
return typeof(Enumerable).GetMethods().First(x => x.Name == "Contains").MakeGenericMethod(tp);
}

private static MethodInfo GetIsNullOrEmptyMethod()
{
return typeof(string).GetMethod("IsNullOrEmpty", new[] { typeof(string) })!;
Expand Down

0 comments on commit a5a7077

Please sign in to comment.