Skip to content

Commit 272745a

Browse files
Support comparison operations for enums (#344)
1 parent 925d7a8 commit 272745a

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,32 @@ public void GuidComparison() {
317317
);
318318
}
319319

320+
321+
[Fact]
322+
public void EnumComparison() {
323+
// https://github.com/DevExpress/DevExtreme.AspNet.Data/issues/164
324+
325+
Assert.Equal(
326+
"(obj.CompareTo(Monday) > 0)",
327+
Compile<DayOfWeek>(new object[] { "this", ">", DayOfWeek.Monday }).Body.ToString()
328+
);
329+
330+
Assert.Equal(
331+
"(obj.Value.CompareTo(Tuesday) < 0)",
332+
Compile<DayOfWeek?>(new object[] { "this", "<", DayOfWeek.Tuesday }).Body.ToString()
333+
);
334+
335+
Assert.Equal(
336+
"IIF((obj == null), False, (obj.Value.CompareTo(Wednesday) >= 0))",
337+
Compile<DayOfWeek?>(new object[] { "this", ">=", DayOfWeek.Wednesday }, true).Body.ToString()
338+
);
339+
340+
Assert.Equal(
341+
"False",
342+
Compile<DayOfWeek?>(new[] { "this", "<=", null }).Body.ToString()
343+
);
344+
}
345+
320346
}
321347

322348
}

net/DevExtreme.AspNet.Data/FilterExpressionCompiler.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,17 @@ Expression CompileBinary(ParameterExpression dataItemExpr, IList criteriaJson) {
8888
if(_stringToLower && clientValue is String)
8989
clientValue = ((string)clientValue).ToLower();
9090

91-
if((accessorExpr.Type == typeof(Guid) || accessorExpr.Type == typeof(Guid?)) && IsInequality(expressionType))
92-
return CompileGuidComparison(accessorExpr, expressionType, clientValue);
91+
if(IsInequality(expressionType)) {
92+
if(accessorExpr.Type == typeof(Guid) || accessorExpr.Type == typeof(Guid?)) {
93+
var method = typeof(Guid).GetMethod(nameof(Guid.CompareTo), new[] { typeof(Guid) });
94+
return CompileCompareToCall(accessorExpr, expressionType, clientValue, method);
95+
}
96+
97+
if(Utils.StripNullableType(accessorExpr.Type).IsEnum) {
98+
var method = typeof(Enum).GetMethod(nameof(Enum.CompareTo), new[] { typeof(object) });
99+
return CompileCompareToCall(accessorExpr, expressionType, clientValue, method);
100+
}
101+
}
93102

94103
Expression valueExpr = Expression.Constant(clientValue, accessorExpr.Type);
95104

@@ -120,16 +129,16 @@ bool IsInequality(ExpressionType type) {
120129
return type == ExpressionType.LessThan || type == ExpressionType.LessThanOrEqual || type == ExpressionType.GreaterThanOrEqual || type == ExpressionType.GreaterThan;
121130
}
122131

123-
Expression CompileGuidComparison(Expression accessorExpr, ExpressionType expressionType, object clientValue) {
132+
Expression CompileCompareToCall(Expression accessorExpr, ExpressionType expressionType, object clientValue, MethodInfo compareToMethod) {
124133
if(clientValue == null)
125134
return Expression.Constant(false);
126135

127136
var result = Expression.MakeBinary(
128137
expressionType,
129138
Expression.Call(
130139
Utils.IsNullable(accessorExpr.Type) ? Expression.Property(accessorExpr, "Value") : accessorExpr,
131-
typeof(Guid).GetMethod(nameof(Guid.CompareTo), new[] { typeof(Guid) }),
132-
Expression.Constant(clientValue, typeof(Guid))
140+
compareToMethod,
141+
Expression.Constant(clientValue, compareToMethod.GetParameters()[0].ParameterType)
133142
),
134143
Expression.Constant(0)
135144
);

0 commit comments

Comments
 (0)