Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix enums could not be cast to decimal #576

Merged
merged 2 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/System.Linq.Dynamic.Core/Parser/ExpressionPromoter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ public virtual Expression Promote(Expression expr, Type type, bool exact, bool c

if (TypeHelper.IsCompatibleWith(expr.Type, type))
{
if (type.GetTypeInfo().IsValueType || exact || expr.Type.GetTypeInfo().IsValueType && convertExpr)
if (type == typeof(decimal) && expr.Type.IsEnum)
{
return Expression.Convert(Expression.Convert(expr, Enum.GetUnderlyingType(expr.Type)), type);
}
else if (type.GetTypeInfo().IsValueType || exact || expr.Type.GetTypeInfo().IsValueType && convertExpr)
{
return Expression.Convert(expr, type);
}
Expand Down
12 changes: 12 additions & 0 deletions test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -681,12 +681,24 @@ public void ExpressionTests_Enum_Property_Equality_Using_Argument()
var resultEqualIntParamLeft = qry.Where("@0 == it.B", 1).ToDynamicArray();
var resultEqualIntParamRight = qry.Where("it.B == @0", 1).ToDynamicArray();

var resultEqualDecimalParamLeft = qry.Where("@0 == it.B", 1m).ToDynamicArray();
var resultEqualDecimalParamRight = qry.Where("it.B == @0", 1m).ToDynamicArray();

var resultEqualDoubleParamLeft = qry.Where("@0 == it.B", 1.0).ToDynamicArray();
var resultEqualDoubleParamRight = qry.Where("it.B == @0", 1.0).ToDynamicArray();

// Assert
Check.That(resultEqualEnumParamLeft.Single()).Equals(TestEnum2.Var2);
Check.That(resultEqualEnumParamRight.Single()).Equals(TestEnum2.Var2);

Check.That(resultEqualIntParamLeft.Single()).Equals(TestEnum2.Var2);
Check.That(resultEqualIntParamRight.Single()).Equals(TestEnum2.Var2);

Check.That(resultEqualDecimalParamLeft.Single()).Equals(TestEnum2.Var2);
Check.That(resultEqualDecimalParamRight.Single()).Equals(TestEnum2.Var2);

Check.That(resultEqualDoubleParamLeft.Single()).Equals(TestEnum2.Var2);
Check.That(resultEqualDoubleParamRight.Single()).Equals(TestEnum2.Var2);
}

[Fact]
Expand Down