System.Linq.Dynamic.Core icon indicating copy to clipboard operation
System.Linq.Dynamic.Core copied to clipboard

UseParameterizedNamesInDynamicQuery not working when parameter is converted

Open SamWhitby opened this issue 5 years ago • 3 comments

Hi,

The setting UseParameterizedNamesInDynamicQuery is not being respected when the parser has to apply a convert.

For example, i have the class with a nullable double property

internal class Person
{
    public double? FavouriteDecimalNumber { get; set; }
}

and i have the following expression that checks for equality against a specific number

var lambda = DynamicExpressionParser.ParseLambda<Person, bool>(new ParsingConfig() { UseParameterizedNamesInDynamicQuery = true }, false, "FavouriteDecimalNumber = 44.8");

The expression that is generated does not warp the value, instead it uses a constant

image

Making the property on the class Person non nullable removes the convert from the generated expression and the value then gets wrappped/parameterized as seen below image

The problem seems to be in here where the code is only checking for the type of ConstantExpression, however in this case the expression comes through as a UnaryExpression.

I'm assuming this can be fixed by calling ConstantExpressionWrapper.Wrap before applying the cast function (but this seem to occur in quite a few places) or enhancing the ConstantExpressionWrapper.Wrap function to handle UnaryExpression types.

SamWhitby avatar Dec 11 '19 01:12 SamWhitby

@SamWhitby Can you try version System.Linq.Dynamic.Core.1.0.20-ci-12330.nupkg from MyGet ?

StefH avatar Dec 11 '19 07:12 StefH

Thanks for the quick response. Unfortunately that does not fix my issue. I grabbed your branch and ran your test here and its passing under that scenario, however if you add a double property to the person object and change the value you are comparing to something like 42.4 you can see a cast is applied to the generated query and the value is not parameterized

image

I also noticed passing a nullable value as a parameter also fails as seen below, not really sure why the convert is even being applied in this case? image

SamWhitby avatar Dec 11 '19 20:12 SamWhitby

There does exist something of a workaround I've found - by explicitly instantiating the nullable constant with its corresponding nullable type constructor:

.Where("NullableDoubleId = double?(42.2)")

For me, this resulted in a correctly parameterized query.

I'll take a look at PR #332 and see what it still needs.

ascott18 avatar Jul 10 '20 17:07 ascott18