diff --git a/src/System.Linq.Dynamic.Core/Parser/TypeHelper.cs b/src/System.Linq.Dynamic.Core/Parser/TypeHelper.cs index 0268ef6a..22e50778 100644 --- a/src/System.Linq.Dynamic.Core/Parser/TypeHelper.cs +++ b/src/System.Linq.Dynamic.Core/Parser/TypeHelper.cs @@ -54,8 +54,8 @@ public static bool IsCompatibleWith(Type source, Type target) return false; } - TypeCode sc = st.GetTypeInfo().IsEnum ? TypeCode.Object : Type.GetTypeCode(st); - TypeCode tc = tt.GetTypeInfo().IsEnum ? TypeCode.Object : Type.GetTypeCode(tt); + TypeCode sc = st.GetTypeInfo().IsEnum ? TypeCode.Int64 : Type.GetTypeCode(st); + TypeCode tc = tt.GetTypeInfo().IsEnum ? TypeCode.Int64 : Type.GetTypeCode(tt); switch (sc) { case TypeCode.SByte: @@ -71,6 +71,7 @@ public static bool IsCompatibleWith(Type source, Type target) return true; } break; + case TypeCode.Byte: switch (tc) { @@ -87,6 +88,7 @@ public static bool IsCompatibleWith(Type source, Type target) return true; } break; + case TypeCode.Int16: switch (tc) { @@ -99,6 +101,7 @@ public static bool IsCompatibleWith(Type source, Type target) return true; } break; + case TypeCode.UInt16: switch (tc) { @@ -113,6 +116,7 @@ public static bool IsCompatibleWith(Type source, Type target) return true; } break; + case TypeCode.Int32: switch (tc) { @@ -124,6 +128,7 @@ public static bool IsCompatibleWith(Type source, Type target) return true; } break; + case TypeCode.UInt32: switch (tc) { @@ -136,6 +141,7 @@ public static bool IsCompatibleWith(Type source, Type target) return true; } break; + case TypeCode.Int64: switch (tc) { @@ -146,6 +152,7 @@ public static bool IsCompatibleWith(Type source, Type target) return true; } break; + case TypeCode.UInt64: switch (tc) { @@ -156,6 +163,7 @@ public static bool IsCompatibleWith(Type source, Type target) return true; } break; + case TypeCode.Single: switch (tc) { @@ -164,6 +172,7 @@ public static bool IsCompatibleWith(Type source, Type target) return true; } break; + default: if (st == tt) { diff --git a/test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs b/test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs index 9173dfe8..9ffe41b0 100644 --- a/test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs +++ b/test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs @@ -61,6 +61,16 @@ public class Foo public string Two(int x, int y) => null; } + public class SourceClass + { + public int A { get; set; } + } + + public class TargetClass + { + public TestEnum A { get; set; } + } + [Fact] public void ExpressionTests_Add_Number() { @@ -598,6 +608,20 @@ public void ExpressionTests_DoubleQualifiers_Negative() Assert.Equal(resultValues.ToArray(), result2.ToArray()); } + [Fact] + public void ExpressionTests_Enum_Cast_Int_To_Enum() + { + // Arrange + var enumvalue = TestEnum.Var1; + var qry = new List { new SourceClass { A = (int)enumvalue } }.AsQueryable(); + + // Act + var result = qry.Select("new(A)").ToArray(); + + // Assert + result.Should().ContainEquivalentOf(new TargetClass { A = enumvalue }); + } + [Fact] public void ExpressionTests_Enum() { @@ -1634,11 +1658,11 @@ public void ExpressionTests_Select_ExpandoObjects() dynamic a = new ExpandoObject(); a.Name = "a"; a.BlogId = 100; - + dynamic b = new ExpandoObject(); b.Name = "b"; b.BlogId = 200; - + var list = new List { a, b }; IQueryable qry = list.AsQueryable();