Skip to content

Commit 943ac32

Browse files
Fix DynamicCompare for DateTime (#391)
1 parent 20635cf commit 943ac32

5 files changed

Lines changed: 35 additions & 24 deletions

File tree

net/DevExtreme.AspNet.Data.Tests/DynamicBindingTests.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,9 @@ void Case(IList clientFilter, string expectedExpr, object trueTestValue) {
225225

226226
Case(
227227
new object[] { "this", ">", new JValue(9) },
228-
"(DynamicCompare(obj, 9) > 0)",
228+
"(DynamicCompare(obj, 9, False) > 0)",
229229
10
230230
);
231-
232-
Case(
233-
new object[] { "this", new JValue("a") },
234-
@"(obj.ToString() == ""a"")",
235-
"a"
236-
);
237231
}
238232

239233
[Fact]
@@ -265,6 +259,17 @@ public void DBNull() {
265259
RequireTotalCount = true
266260
}).totalCount);
267261
}
262+
263+
[Fact]
264+
public void T819075() {
265+
dynamic sourceItem = new ExpandoObject();
266+
sourceItem.p = new DateTime(2011, 11, 11);
267+
268+
Assert.Equal(1, DataSourceLoader.Load(new[] { sourceItem }, new SampleLoadOptions {
269+
Filter = new[] { "p", "11/11/2011" },
270+
RequireTotalCount = true
271+
}).totalCount);
272+
}
268273
}
269274

270275
}

net/DevExtreme.AspNet.Data.Tests/StringToLowerTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ public void Dynamic() {
5757
var expr = compiler.Compile(new object[] {
5858
new[] { "this", "startswith", "1" },
5959
"or",
60-
new[] { "this", "b" },
60+
new[] { "this", "B" },
6161
"or",
62-
new[] { "this", ">=", "c" }
62+
new[] { "this", ">=", "C" }
6363
});
6464

6565
var expectedExpr = "(((IIF((obj == null), null, obj.ToString().ToLower()) ?? '').StartsWith('1')"
66-
+ " OrElse (IIF((obj == null), null, obj.ToString().ToLower()) == 'b'))"
67-
+ " OrElse (Compare(IIF((obj == null), null, obj.ToString().ToLower()), 'c') >= 0))";
66+
+ " OrElse (DynamicCompare(obj, 'b', True) == 0))"
67+
+ " OrElse (DynamicCompare(obj, 'c', True) >= 0))";
6868

6969
Assert.Equal(
7070
expectedExpr.Replace("'", "\""),
@@ -73,8 +73,8 @@ public void Dynamic() {
7373

7474
var method = expr.Compile();
7575
Assert.True((bool)method.DynamicInvoke(1));
76-
Assert.True((bool)method.DynamicInvoke("b"));
77-
Assert.True((bool)method.DynamicInvoke('c'));
76+
Assert.True((bool)method.DynamicInvoke("B"));
77+
Assert.True((bool)method.DynamicInvoke('C'));
7878
}
7979

8080
[Fact]

net/DevExtreme.AspNet.Data/DynamicBindingHelper.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
using System.Dynamic;
55
using System.Linq;
66
using System.Linq.Expressions;
7-
using System.Reflection;
8-
using System.Text;
97
using DynamicBinder = Microsoft.CSharp.RuntimeBinder.Binder;
108

119
namespace DevExtreme.AspNet.Data {
1210

1311
static class DynamicBindingHelper {
14-
static IEnumerable<CSharpArgumentInfo> EMPTY_ARGUMENT_INFO;
12+
readonly static IEnumerable<CSharpArgumentInfo> EMPTY_ARGUMENT_INFO = new[] {
13+
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
14+
};
1515

1616
public static bool ShouldUseDynamicBinding(Type type) {
1717
if(type == typeof(object))
@@ -29,9 +29,6 @@ public static bool ShouldUseDynamicBinding(Type type) {
2929
}
3030

3131
public static Expression CompileGetMember(Expression target, string clientExpr) {
32-
if(EMPTY_ARGUMENT_INFO == null)
33-
EMPTY_ARGUMENT_INFO = new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) };
34-
3532
var binder = DynamicBinder.GetMember(CSharpBinderFlags.None, clientExpr, typeof(DynamicBindingHelper), EMPTY_ARGUMENT_INFO);
3633

3734
return Expression.Call(

net/DevExtreme.AspNet.Data/FilterExpressionCompiler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Expression CompileBinary(ParameterExpression dataItemExpr, IList criteriaJson) {
6161
}
6262

6363
var accessorExpr = CompileAccessorExpression(dataItemExpr, clientAccessor, progression => {
64-
if(isStringOperation || progression.Last().Type == typeof(Object) && clientValue is String)
64+
if(isStringOperation)
6565
ForceToString(progression);
6666

6767
if(_stringToLower)
@@ -128,7 +128,7 @@ Expression CompileBinary(ParameterExpression dataItemExpr, IList criteriaJson) {
128128
var compareMethod = typeof(Utils).GetMethod(nameof(Utils.DynamicCompare));
129129
return Expression.MakeBinary(
130130
expressionType,
131-
Expression.Call(compareMethod, accessorExpr, valueExpr),
131+
Expression.Call(compareMethod, accessorExpr, valueExpr, Expression.Constant(_stringToLower)),
132132
Expression.Constant(0)
133133
);
134134
}

net/DevExtreme.AspNet.Data/Utils.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,23 @@ public static string[] GetPrimaryKey(Type type) {
9090
.ToArray();
9191
}
9292

93-
public static int DynamicCompare(object selectorResult, object clientValue) {
93+
public static int DynamicCompare(object selectorResult, object clientValue, bool stringToLower) {
9494
if(selectorResult is DBNull)
9595
selectorResult = null;
9696

97-
if(selectorResult != null)
97+
if(selectorResult != null) {
9898
clientValue = ConvertClientValue(clientValue, selectorResult.GetType());
99-
else
99+
100+
if(stringToLower && clientValue != null) {
101+
if(selectorResult is String selectorResultString) {
102+
selectorResult = selectorResultString.ToLower();
103+
} else if(selectorResult is Char selectorResultChar) {
104+
selectorResult = Char.ToLower(selectorResultChar);
105+
}
106+
}
107+
} else {
100108
clientValue = UnwrapNewtonsoftValue(clientValue);
109+
}
101110

102111
return Comparer<object>.Default.Compare(selectorResult, clientValue);
103112
}

0 commit comments

Comments
 (0)