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

Support strings as Enum Parameter Objects #37

Merged
merged 4 commits into from
Jul 22, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/System.Linq.Dynamic.Core/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -672,12 +672,28 @@ Expression ParseComparison()
}
else if (IsEnumType(left.Type) && (constantExpr = right as ConstantExpression) != null)
{
var wrt = Enum.ToObject(left.Type, constantExpr.Value);
object wrt = null;
if (constantExpr.Value is string)
{
wrt = Enum.Parse(left.Type, constantExpr.Value as string, false);
}
else
{
wrt = Enum.ToObject(left.Type, constantExpr.Value);
}
right = Expression.Constant(wrt, left.Type);
}
else if (IsEnumType(right.Type) && (constantExpr = left as ConstantExpression) != null)
{
var wrt = Enum.ToObject(right.Type, constantExpr.Value);
object wrt = null;
if (constantExpr.Value is string)
{
wrt = Enum.Parse(right.Type, constantExpr.Value as string, false);
}
else
{
wrt = Enum.ToObject(right.Type, constantExpr.Value);
}
left = Expression.Constant(wrt, right.Type);
}
else
Expand Down Expand Up @@ -2725,4 +2741,4 @@ internal static void ResetDynamicLinqTypes()
_keywords = null;
}
}
}
}
2 changes: 2 additions & 0 deletions test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,13 +458,15 @@ public void ExpressionTests_Enum()
var result3 = qry.Where("it = Var5");
var result4 = qry.Where("it = @0", TestEnum.Var5);
var result5 = qry.Where("it = @0", 8);
var result6 = qry.Where("it = @0", "Var5");

//Assert
Assert.Equal(new[] { TestEnum.Var1, TestEnum.Var2, TestEnum.Var3 }, result1.ToArray());
Assert.Equal(new[] { TestEnum.Var1, TestEnum.Var2, TestEnum.Var3 }, result2.ToArray());
Assert.Equal(TestEnum.Var5, result3.Single());
Assert.Equal(TestEnum.Var5, result4.Single());
Assert.Equal(TestEnum.Var5, result5.Single());
Assert.Equal(TestEnum.Var5, result6.Single());
}

[Fact]
Expand Down