Skip to content

Commit

Permalink
Fix the usage of ParsingConfig in some methods in the DynamicQueryabl…
Browse files Browse the repository at this point in the history
…eExtensions class (#883)

* Fix the usage of ParsingConfig in some methods in the DynamicQueryableExtensions class

* 1 more

* UT
  • Loading branch information
StefH authored Jan 25, 2025
1 parent bcd8f7b commit cb09b72
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>(_AllPredicate, source, Expression.Quote(lambda));
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1806,7 +1806,7 @@ public static IQueryable<TResult> Select<TResult>(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)
);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1980,7 +1980,7 @@ public static IQueryable<TResult> SelectMany<TResult>(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<T> instead of whatever the actual property is.
Type inputType = source.Expression.Type.GetTypeInfo().GetGenericTypeArguments()[0];
Expand Down Expand Up @@ -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))
);

Expand Down Expand Up @@ -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)!;
}

Expand Down Expand Up @@ -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)!;
}

Expand Down Expand Up @@ -2566,15 +2566,15 @@ 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
{
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));
}
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -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
Expand Down

0 comments on commit cb09b72

Please sign in to comment.