Skip to content

Commit c7fc9be

Browse files
authored
Fix "in" for nullable Enums (#932)
1 parent 3208964 commit c7fc9be

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,8 @@ private Expression ParseIn()
374374
// we need to parse unary expressions because otherwise 'in' clause will fail in use cases like 'in (-1, -1)' or 'in (!true)'
375375
Expression right = ParseUnary();
376376

377-
// if the identifier is an Enum, try to convert the right-side also to an Enum.
378-
if (left.Type.GetTypeInfo().IsEnum)
377+
// if the identifier is an Enum (or nullable Enum), try to convert the right-side also to an Enum.
378+
if (TypeHelper.GetNonNullableType(left.Type).GetTypeInfo().IsEnum)
379379
{
380380
if (right is ConstantExpression constantExprRight)
381381
{

test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,10 +1269,6 @@ public void ExpressionTests_HexadecimalInteger()
12691269
[Fact]
12701270
public void ExpressionTests_In_Enum()
12711271
{
1272-
var config = new ParsingConfig();
1273-
#if NETSTANDARD
1274-
// config.CustomTypeProvider = new NetStandardCustomTypeProvider();
1275-
#endif
12761272
// Arrange
12771273
var model1 = new ModelWithEnum { TestEnum = TestEnum.Var1 };
12781274
var model2 = new ModelWithEnum { TestEnum = TestEnum.Var2 };
@@ -1281,8 +1277,28 @@ public void ExpressionTests_In_Enum()
12811277

12821278
// Act
12831279
var expected = qry.Where(x => new[] { TestEnum.Var1, TestEnum.Var2 }.Contains(x.TestEnum)).ToArray();
1284-
var result1 = qry.Where(config, "it.TestEnum in (\"Var1\", \"Var2\")").ToArray();
1285-
var result2 = qry.Where(config, "it.TestEnum in (0, 1)").ToArray();
1280+
var result1 = qry.Where("it.TestEnum in (\"Var1\", \"Var2\")").ToArray();
1281+
var result2 = qry.Where("it.TestEnum in (0, 1)").ToArray();
1282+
1283+
// Assert
1284+
Check.That(result1).ContainsExactly(expected);
1285+
Check.That(result2).ContainsExactly(expected);
1286+
}
1287+
1288+
[Fact]
1289+
public void ExpressionTests_In_EnumIsNullable()
1290+
{
1291+
// Arrange
1292+
var model1 = new ModelWithEnum { TestEnumNullable = TestEnum.Var1 };
1293+
var model2 = new ModelWithEnum { TestEnumNullable = TestEnum.Var2 };
1294+
var model3 = new ModelWithEnum { TestEnumNullable = TestEnum.Var3 };
1295+
var model4 = new ModelWithEnum { TestEnumNullable = null };
1296+
var qry = new[] { model1, model2, model3, model4 }.AsQueryable();
1297+
1298+
// Act
1299+
var expected = new[] { model1, model2 };
1300+
var result1 = qry.Where("it.TestEnumNullable in (\"Var1\", \"Var2\")").ToArray();
1301+
var result2 = qry.Where("it.TestEnumNullable in (0, 1)").ToArray();
12861302

12871303
// Assert
12881304
Check.That(result1).ContainsExactly(expected);
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-

2-
namespace System.Linq.Dynamic.Core.Tests.Helpers.Models
1+
namespace System.Linq.Dynamic.Core.Tests.Helpers.Models;
2+
3+
public class ModelWithEnum
34
{
4-
public class ModelWithEnum
5-
{
6-
public string Name { get; set; }
5+
public string Name { get; set; } = null!;
6+
7+
public TestEnum TestEnum { get; set; }
78

8-
public TestEnum TestEnum { get; set; }
9-
}
9+
public TestEnum? TestEnumNullable { get; set; }
1010
}

0 commit comments

Comments
 (0)