diff --git a/src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs b/src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs index b4a7245f..eba7fffc 100644 --- a/src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs +++ b/src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs @@ -374,8 +374,8 @@ private Expression ParseIn() // we need to parse unary expressions because otherwise 'in' clause will fail in use cases like 'in (-1, -1)' or 'in (!true)' Expression right = ParseUnary(); - // if the identifier is an Enum, try to convert the right-side also to an Enum. - if (left.Type.GetTypeInfo().IsEnum) + // if the identifier is an Enum (or nullable Enum), try to convert the right-side also to an Enum. + if (TypeHelper.GetNonNullableType(left.Type).GetTypeInfo().IsEnum) { if (right is ConstantExpression constantExprRight) { diff --git a/test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs b/test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs index d71845ea..021183a5 100644 --- a/test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs +++ b/test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs @@ -1269,10 +1269,6 @@ public void ExpressionTests_HexadecimalInteger() [Fact] public void ExpressionTests_In_Enum() { - var config = new ParsingConfig(); -#if NETSTANDARD - // config.CustomTypeProvider = new NetStandardCustomTypeProvider(); -#endif // Arrange var model1 = new ModelWithEnum { TestEnum = TestEnum.Var1 }; var model2 = new ModelWithEnum { TestEnum = TestEnum.Var2 }; @@ -1281,8 +1277,28 @@ public void ExpressionTests_In_Enum() // Act var expected = qry.Where(x => new[] { TestEnum.Var1, TestEnum.Var2 }.Contains(x.TestEnum)).ToArray(); - var result1 = qry.Where(config, "it.TestEnum in (\"Var1\", \"Var2\")").ToArray(); - var result2 = qry.Where(config, "it.TestEnum in (0, 1)").ToArray(); + var result1 = qry.Where("it.TestEnum in (\"Var1\", \"Var2\")").ToArray(); + var result2 = qry.Where("it.TestEnum in (0, 1)").ToArray(); + + // Assert + Check.That(result1).ContainsExactly(expected); + Check.That(result2).ContainsExactly(expected); + } + + [Fact] + public void ExpressionTests_In_EnumIsNullable() + { + // Arrange + var model1 = new ModelWithEnum { TestEnumNullable = TestEnum.Var1 }; + var model2 = new ModelWithEnum { TestEnumNullable = TestEnum.Var2 }; + var model3 = new ModelWithEnum { TestEnumNullable = TestEnum.Var3 }; + var model4 = new ModelWithEnum { TestEnumNullable = null }; + var qry = new[] { model1, model2, model3, model4 }.AsQueryable(); + + // Act + var expected = new[] { model1, model2 }; + var result1 = qry.Where("it.TestEnumNullable in (\"Var1\", \"Var2\")").ToArray(); + var result2 = qry.Where("it.TestEnumNullable in (0, 1)").ToArray(); // Assert Check.That(result1).ContainsExactly(expected); diff --git a/test/System.Linq.Dynamic.Core.Tests/Helpers/Models/ModelWithEnum.cs b/test/System.Linq.Dynamic.Core.Tests/Helpers/Models/ModelWithEnum.cs index 415997a6..3a54b692 100644 --- a/test/System.Linq.Dynamic.Core.Tests/Helpers/Models/ModelWithEnum.cs +++ b/test/System.Linq.Dynamic.Core.Tests/Helpers/Models/ModelWithEnum.cs @@ -1,10 +1,10 @@ - -namespace System.Linq.Dynamic.Core.Tests.Helpers.Models +namespace System.Linq.Dynamic.Core.Tests.Helpers.Models; + +public class ModelWithEnum { - public class ModelWithEnum - { - public string Name { get; set; } + public string Name { get; set; } = null!; + + public TestEnum TestEnum { get; set; } - public TestEnum TestEnum { get; set; } - } + public TestEnum? TestEnumNullable { get; set; } } \ No newline at end of file diff --git a/test/System.Linq.Dynamic.Core.Tests/QueryableTests.UseParameterizedNamesInDynamicQuery .cs b/test/System.Linq.Dynamic.Core.Tests/QueryableTests.UseParameterizedNamesInDynamicQuery.cs similarity index 100% rename from test/System.Linq.Dynamic.Core.Tests/QueryableTests.UseParameterizedNamesInDynamicQuery .cs rename to test/System.Linq.Dynamic.Core.Tests/QueryableTests.UseParameterizedNamesInDynamicQuery.cs