Skip to content

Commit 803ee4d

Browse files
committed
Fixed : calling methods which returns a nullable (#382)
1 parent c8841b0 commit 803ee4d

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,14 @@ public static bool IsPredefinedType(ParsingConfig config, Type type)
8888
Check.NotNull(config, nameof(config));
8989
Check.NotNull(type, nameof(type));
9090

91-
if (PredefinedTypes.ContainsKey(type))
91+
var nonNullableType = TypeHelper.GetNonNullableType(type);
92+
if (PredefinedTypes.ContainsKey(nonNullableType))
9293
{
9394
return true;
9495
}
9596

96-
return config.CustomTypeProvider != null && config.CustomTypeProvider.GetCustomTypes().Contains(type);
97+
return config.CustomTypeProvider != null &&
98+
(config.CustomTypeProvider.GetCustomTypes().Contains(type) || config.CustomTypeProvider.GetCustomTypes().Contains(nonNullableType));
9799
}
98100
}
99101
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Xunit;
2+
3+
namespace System.Linq.Dynamic.Core.Tests
4+
{
5+
public partial class QueryableTests
6+
{
7+
[Fact]
8+
public void CallMethod()
9+
{
10+
// Arrange
11+
var query = new[] { new Test() }.AsQueryable();
12+
13+
// Act
14+
var result = query.Select<decimal>("t => t.GetDecimal()").First();
15+
16+
// Assert
17+
Assert.Equal(42, result);
18+
}
19+
20+
[Fact]
21+
public void CallMethodWhichReturnsNullable()
22+
{
23+
// Arrange
24+
var query = new[] { new Test() }.AsQueryable();
25+
26+
// Act
27+
var result = query.Select<decimal?>("t => t.GetNullableDecimal()").First();
28+
29+
// Assert
30+
Assert.Equal(null, result);
31+
}
32+
33+
[Fact]
34+
public void CallMethodWhichReturnsNullable_WithValue()
35+
{
36+
// Arrange
37+
var query = new[] { new Test() }.AsQueryable();
38+
39+
// Act
40+
var result = query.Select<decimal?>("t => t.GetNullableDecimalWithValue()").First();
41+
42+
// Assert
43+
Assert.Equal(100, result);
44+
}
45+
}
46+
47+
class Test
48+
{
49+
public decimal GetDecimal()
50+
{
51+
return 42;
52+
}
53+
54+
public decimal? GetNullableDecimal()
55+
{
56+
return null;
57+
}
58+
59+
public decimal? GetNullableDecimalWithValue()
60+
{
61+
return 100;
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)