diff --git a/src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs b/src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs index 917dc745..65365eae 100644 --- a/src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs +++ b/src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs @@ -127,7 +127,7 @@ public static bool All(this IQueryable source, ParsingConfig config, string pred Check.NotEmpty(predicate); bool createParameterCtor = SupportsLinqToObjects(config, source); - LambdaExpression lambda = DynamicExpressionParser.ParseLambda(createParameterCtor, source.ElementType, null, predicate, args); + LambdaExpression lambda = DynamicExpressionParser.ParseLambda(config, createParameterCtor, source.ElementType, null, predicate, args); return Execute(_AllPredicate, source, Expression.Quote(lambda)); } @@ -1430,7 +1430,7 @@ public static IQueryable OfType(this IQueryable source, Type type) Check.NotNull(source); Check.NotNull(type); - var optimized = OptimizeExpression(Expression.Call(null, _ofType.MakeGenericMethod(type), new[] { source.Expression })); + var optimized = OptimizeExpression(Expression.Call(null, _ofType.MakeGenericMethod(type), [source.Expression])); return source.Provider.CreateQuery(optimized); } @@ -1547,7 +1547,7 @@ public static IOrderedQueryable OrderBy(this IQueryable source, ParsingConfig co { if (args.Length > 0 && args[0] != null && args[0]!.GetType().GetInterfaces().Any(i => i.Name.Contains("IComparer`1"))) { - return InternalOrderBy(source, ParsingConfig.Default, ordering, args[0]!, args); + return InternalOrderBy(source, config, ordering, args[0]!, args); } return InternalOrderBy(source, config, ordering, null, args); @@ -1806,7 +1806,7 @@ public static IQueryable Select(this IQueryable source, Parsin var methodCallExpression = Expression.Call( typeof(Queryable), nameof(Queryable.Select), - new[] { source.ElementType, typeof(TResult) }, + [source.ElementType, typeof(TResult)], source.Expression, Expression.Quote(lambda) ); @@ -1849,7 +1849,7 @@ public static IQueryable Select(this IQueryable source, ParsingConfig config, Ty var optimized = OptimizeExpression(Expression.Call( typeof(Queryable), nameof(Queryable.Select), - new[] { source.ElementType, resultType }, + [source.ElementType, resultType], source.Expression, Expression.Quote(lambda))); return source.Provider.CreateQuery(optimized); @@ -1905,7 +1905,7 @@ public static IQueryable SelectMany(this IQueryable source, ParsingConfig config { Check.NotNull(source); Check.NotNull(config); - Check.NotNull(resultType, nameof(resultType)); + Check.NotNull(resultType); Check.NotEmpty(selector); return SelectManyInternal(source, config, resultType, selector, args); @@ -1980,7 +1980,7 @@ public static IQueryable SelectMany(this IQueryable source, Pa Check.NotEmpty(selector); bool createParameterCtor = config.EvaluateGroupByAtDatabase || SupportsLinqToObjects(config, source); - LambdaExpression lambda = DynamicExpressionParser.ParseLambda(createParameterCtor, source.ElementType, null, selector, args); + LambdaExpression lambda = DynamicExpressionParser.ParseLambda(config, createParameterCtor, source.ElementType, null, selector, args); //we have to adjust to lambda to return an IEnumerable instead of whatever the actual property is. Type inputType = source.Expression.Type.GetTypeInfo().GetGenericTypeArguments()[0]; @@ -2096,12 +2096,12 @@ public static IQueryable SelectMany( ParameterExpression xParameter = ParameterExpressionHelper.CreateParameterExpression(source.ElementType, collectionParameterName, config.RenameEmptyParameterExpressionNames); ParameterExpression yParameter = ParameterExpressionHelper.CreateParameterExpression(sourceLambdaResultType, resultParameterName, config.RenameEmptyParameterExpressionNames); - LambdaExpression resultSelectLambda = DynamicExpressionParser.ParseLambda(config, createParameterCtor, new[] { xParameter, yParameter }, null, resultSelector, resultSelectorArgs); + LambdaExpression resultSelectLambda = DynamicExpressionParser.ParseLambda(config, createParameterCtor, [xParameter, yParameter], null, resultSelector, resultSelectorArgs); Type resultLambdaResultType = resultSelectLambda.Body.Type; var optimized = OptimizeExpression(Expression.Call( typeof(Queryable), nameof(Queryable.SelectMany), - new[] { source.ElementType, sourceLambdaResultType, resultLambdaResultType }, + [source.ElementType, sourceLambdaResultType, resultLambdaResultType], source.Expression, Expression.Quote(sourceSelectLambda), Expression.Quote(resultSelectLambda)) ); @@ -2134,7 +2134,7 @@ public static dynamic Single(this IQueryable source) { Check.NotNull(source); - var optimized = OptimizeExpression(Expression.Call(typeof(Queryable), nameof(Queryable.Single), new[] { source.ElementType }, source.Expression)); + var optimized = OptimizeExpression(Expression.Call(typeof(Queryable), nameof(Queryable.Single), [source.ElementType], source.Expression)); return source.Provider.Execute(optimized)!; } @@ -2205,7 +2205,7 @@ public static dynamic SingleOrDefault(this IQueryable source) { Check.NotNull(source); - var optimized = OptimizeExpression(Expression.Call(typeof(Queryable), nameof(Queryable.SingleOrDefault), new[] { source.ElementType }, source.Expression)); + var optimized = OptimizeExpression(Expression.Call(typeof(Queryable), nameof(Queryable.SingleOrDefault), [source.ElementType], source.Expression)); return source.Provider.Execute(optimized)!; } @@ -2566,7 +2566,7 @@ internal static IOrderedQueryable InternalThenBy(IOrderedQueryable source, Parsi { queryExpr = Expression.Call( typeof(Queryable), dynamicOrdering.MethodName, - new[] { source.ElementType, dynamicOrdering.Selector.Type }, + [source.ElementType, dynamicOrdering.Selector.Type], queryExpr, Expression.Quote(Expression.Lambda(dynamicOrdering.Selector, parameters))); } else @@ -2574,7 +2574,7 @@ internal static IOrderedQueryable InternalThenBy(IOrderedQueryable source, Parsi var comparerGenericType = typeof(IComparer<>).MakeGenericType(dynamicOrdering.Selector.Type); queryExpr = Expression.Call( typeof(Queryable), dynamicOrdering.MethodName, - new[] { source.ElementType, dynamicOrdering.Selector.Type }, + [source.ElementType, dynamicOrdering.Selector.Type], queryExpr, Expression.Quote(Expression.Lambda(dynamicOrdering.Selector, parameters)), Expression.Constant(comparer, comparerGenericType)); } @@ -2653,7 +2653,7 @@ public static IQueryable Where(this IQueryable source, ParsingConfig config, str bool createParameterCtor = SupportsLinqToObjects(config, source); LambdaExpression lambda = DynamicExpressionParser.ParseLambda(config, createParameterCtor, source.ElementType, null, predicate, args); - var optimized = OptimizeExpression(Expression.Call(typeof(Queryable), nameof(Queryable.Where), new[] { source.ElementType }, source.Expression, Expression.Quote(lambda))); + var optimized = OptimizeExpression(Expression.Call(typeof(Queryable), nameof(Queryable.Where), [source.ElementType], source.Expression, Expression.Quote(lambda))); return source.Provider.CreateQuery(optimized); } diff --git a/test/System.Linq.Dynamic.Core.Tests/Parser/ExpressionParserTests.MemberAccess.cs b/test/System.Linq.Dynamic.Core.Tests/Parser/ExpressionParserTests.MemberAccess.cs index f4eef1bf..daf62b3d 100644 --- a/test/System.Linq.Dynamic.Core.Tests/Parser/ExpressionParserTests.MemberAccess.cs +++ b/test/System.Linq.Dynamic.Core.Tests/Parser/ExpressionParserTests.MemberAccess.cs @@ -26,6 +26,7 @@ public void ParseMemberAccess_DictionaryIndex_On_Dynamic() [Theory] [InlineData("Prop", "TestProp")] [InlineData("Field", "TestField")] + [InlineData("Constant", "ConstantField")] public void Parse_StaticPropertyOrField_In_StaticClass1(string name, string value) { // Arrange @@ -41,6 +42,7 @@ public void Parse_StaticPropertyOrField_In_StaticClass1(string name, string valu [Theory] [InlineData("Prop", "TestProp")] [InlineData("Field", "TestField")] + [InlineData("Constant", "ConstantField")] public void Parse_StaticPropertyOrField_In_NonStaticClass1(string name, string value) { // Arrange @@ -56,6 +58,7 @@ public void Parse_StaticPropertyOrField_In_NonStaticClass1(string name, string v [Theory] [InlineData("Prop", "TestProp")] [InlineData("Field", "TestField")] + [InlineData("Constant", "ConstantField")] public void Parse_StaticPropertyOrField_In_NonStaticClass2(string name, string value) { // Arrange @@ -75,6 +78,8 @@ public class StaticClassExample public static string Prop { get; set; } = "TestProp"; public static string Field = "TestField"; + + public const string Constant = "ConstantField"; } [DynamicLinqType] @@ -83,6 +88,8 @@ public class NonStaticClassExample public static string Prop { get; set; } = "TestProp"; public static string Field = "TestField"; + + public const string Constant = "ConstantField"; } public class ProductDynamic