Skip to content

Commit

Permalink
Fixed selecting int property into enum property #490
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Apr 2, 2021
1 parent ebb6b94 commit d542b8a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/System.Linq.Dynamic.Core/Parser/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -71,6 +71,7 @@ public static bool IsCompatibleWith(Type source, Type target)
return true;
}
break;

case TypeCode.Byte:
switch (tc)
{
Expand All @@ -87,6 +88,7 @@ public static bool IsCompatibleWith(Type source, Type target)
return true;
}
break;

case TypeCode.Int16:
switch (tc)
{
Expand All @@ -99,6 +101,7 @@ public static bool IsCompatibleWith(Type source, Type target)
return true;
}
break;

case TypeCode.UInt16:
switch (tc)
{
Expand All @@ -113,6 +116,7 @@ public static bool IsCompatibleWith(Type source, Type target)
return true;
}
break;

case TypeCode.Int32:
switch (tc)
{
Expand All @@ -124,6 +128,7 @@ public static bool IsCompatibleWith(Type source, Type target)
return true;
}
break;

case TypeCode.UInt32:
switch (tc)
{
Expand All @@ -136,6 +141,7 @@ public static bool IsCompatibleWith(Type source, Type target)
return true;
}
break;

case TypeCode.Int64:
switch (tc)
{
Expand All @@ -146,6 +152,7 @@ public static bool IsCompatibleWith(Type source, Type target)
return true;
}
break;

case TypeCode.UInt64:
switch (tc)
{
Expand All @@ -156,6 +163,7 @@ public static bool IsCompatibleWith(Type source, Type target)
return true;
}
break;

case TypeCode.Single:
switch (tc)
{
Expand All @@ -164,6 +172,7 @@ public static bool IsCompatibleWith(Type source, Type target)
return true;
}
break;

default:
if (st == tt)
{
Expand Down
28 changes: 26 additions & 2 deletions test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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<SourceClass> { new SourceClass { A = (int)enumvalue } }.AsQueryable();

// Act
var result = qry.Select<TargetClass>("new(A)").ToArray();

// Assert
result.Should().ContainEquivalentOf(new TargetClass { A = enumvalue });
}

[Fact]
public void ExpressionTests_Enum()
{
Expand Down Expand Up @@ -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<dynamic> { a, b };
IQueryable qry = list.AsQueryable();

Expand Down

0 comments on commit d542b8a

Please sign in to comment.