From a5a7077dbb2648fbad5099b2bf6cb58ec8218ea0 Mon Sep 17 00:00:00 2001 From: AliReZa Sabouri Date: Sat, 22 Jun 2024 17:53:48 +0200 Subject: [PATCH] refactor: LinqQueryBuilder.cs --- src/Gridify/QueryBuilders/LinqQueryBuilder.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Gridify/QueryBuilders/LinqQueryBuilder.cs b/src/Gridify/QueryBuilders/LinqQueryBuilder.cs index a4e91e47..8584f04f 100644 --- a/src/Gridify/QueryBuilders/LinqQueryBuilder.cs +++ b/src/Gridify/QueryBuilders/LinqQueryBuilder.cs @@ -27,7 +27,7 @@ internal class LinqQueryBuilder(IGridifyMapper mapper) : BaseQueryBuilder< if (conditionExp is not LambdaExpression lambdaExp) return null; - return LinqQueryBuilder.ParseMethodCallExpression(selectExp, lambdaExp, op) as Expression>; + return ParseMethodCallExpression(selectExp, lambdaExp, op) as Expression>; } case ConditionalExpression cExp: { @@ -269,7 +269,7 @@ protected override Expression> CombineWithOrOperator(Expression.ParseMethodCallExpression(selectExp, lambdaExp, op) as Expression>; + return ParseMethodCallExpression(selectExp, lambdaExp, op) as Expression>; } case ConditionalExpression cExp: { @@ -316,7 +316,7 @@ when subExp.Arguments.Last() is LambdaExpression { Body: MemberExpression lambdaMember }: { var newPredicate = GetAnyExpression(lambdaMember, predicate); - return LinqQueryBuilder.ParseMethodCallExpression(subExp, newPredicate, op); + return ParseMethodCallExpression(subExp, newPredicate, op); } case MethodCallExpression { Method.Name: "Select" } subExp when subExp.Arguments.Last() is LambdaExpression @@ -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.ParseMethodCallExpression(subExp, newPredicate, op); + return ParseMethodCallExpression(subExp, newPredicate, op); } default: throw new InvalidOperationException(); @@ -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) { @@ -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 @@ -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) })!;