Skip to content

Commit 09657b9

Browse files
authored
Fixed selecting int property into enum property #490 (#496)
1 parent ebb6b94 commit 09657b9

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public static bool IsCompatibleWith(Type source, Type target)
5454
return false;
5555
}
5656

57-
TypeCode sc = st.GetTypeInfo().IsEnum ? TypeCode.Object : Type.GetTypeCode(st);
58-
TypeCode tc = tt.GetTypeInfo().IsEnum ? TypeCode.Object : Type.GetTypeCode(tt);
57+
TypeCode sc = st.GetTypeInfo().IsEnum ? TypeCode.Int64 : Type.GetTypeCode(st);
58+
TypeCode tc = tt.GetTypeInfo().IsEnum ? TypeCode.Int64 : Type.GetTypeCode(tt);
5959
switch (sc)
6060
{
6161
case TypeCode.SByte:
@@ -71,6 +71,7 @@ public static bool IsCompatibleWith(Type source, Type target)
7171
return true;
7272
}
7373
break;
74+
7475
case TypeCode.Byte:
7576
switch (tc)
7677
{
@@ -87,6 +88,7 @@ public static bool IsCompatibleWith(Type source, Type target)
8788
return true;
8889
}
8990
break;
91+
9092
case TypeCode.Int16:
9193
switch (tc)
9294
{
@@ -99,6 +101,7 @@ public static bool IsCompatibleWith(Type source, Type target)
99101
return true;
100102
}
101103
break;
104+
102105
case TypeCode.UInt16:
103106
switch (tc)
104107
{
@@ -113,6 +116,7 @@ public static bool IsCompatibleWith(Type source, Type target)
113116
return true;
114117
}
115118
break;
119+
116120
case TypeCode.Int32:
117121
switch (tc)
118122
{
@@ -124,6 +128,7 @@ public static bool IsCompatibleWith(Type source, Type target)
124128
return true;
125129
}
126130
break;
131+
127132
case TypeCode.UInt32:
128133
switch (tc)
129134
{
@@ -136,6 +141,7 @@ public static bool IsCompatibleWith(Type source, Type target)
136141
return true;
137142
}
138143
break;
144+
139145
case TypeCode.Int64:
140146
switch (tc)
141147
{
@@ -146,6 +152,7 @@ public static bool IsCompatibleWith(Type source, Type target)
146152
return true;
147153
}
148154
break;
155+
149156
case TypeCode.UInt64:
150157
switch (tc)
151158
{
@@ -156,6 +163,7 @@ public static bool IsCompatibleWith(Type source, Type target)
156163
return true;
157164
}
158165
break;
166+
159167
case TypeCode.Single:
160168
switch (tc)
161169
{
@@ -164,6 +172,7 @@ public static bool IsCompatibleWith(Type source, Type target)
164172
return true;
165173
}
166174
break;
175+
167176
default:
168177
if (st == tt)
169178
{

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

+26-2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ public class Foo
6161
public string Two(int x, int y) => null;
6262
}
6363

64+
public class SourceClass
65+
{
66+
public int A { get; set; }
67+
}
68+
69+
public class TargetClass
70+
{
71+
public TestEnum A { get; set; }
72+
}
73+
6474
[Fact]
6575
public void ExpressionTests_Add_Number()
6676
{
@@ -598,6 +608,20 @@ public void ExpressionTests_DoubleQualifiers_Negative()
598608
Assert.Equal(resultValues.ToArray(), result2.ToArray());
599609
}
600610

611+
[Fact]
612+
public void ExpressionTests_Enum_Cast_Int_To_Enum()
613+
{
614+
// Arrange
615+
var enumvalue = TestEnum.Var1;
616+
var qry = new List<SourceClass> { new SourceClass { A = (int)enumvalue } }.AsQueryable();
617+
618+
// Act
619+
var result = qry.Select<TargetClass>("new(A)").ToArray();
620+
621+
// Assert
622+
result.Should().ContainEquivalentOf(new TargetClass { A = enumvalue });
623+
}
624+
601625
[Fact]
602626
public void ExpressionTests_Enum()
603627
{
@@ -1634,11 +1658,11 @@ public void ExpressionTests_Select_ExpandoObjects()
16341658
dynamic a = new ExpandoObject();
16351659
a.Name = "a";
16361660
a.BlogId = 100;
1637-
1661+
16381662
dynamic b = new ExpandoObject();
16391663
b.Name = "b";
16401664
b.BlogId = 200;
1641-
1665+
16421666
var list = new List<dynamic> { a, b };
16431667
IQueryable qry = list.AsQueryable();
16441668

0 commit comments

Comments
 (0)