Skip to content

Commit 946dba7

Browse files
committed
PartialIndexFilterBuilder handles filters with enum constants
1 parent 66d50e5 commit 946dba7

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

Orm/Xtensive.Orm/Orm/Building/Builders/PartialIndexFilterBuilder.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Copyright (C) 2011 Xtensive LLC.
2-
// All rights reserved.
3-
// For conditions of distribution and use, see license.
1+
// Copyright (C) 2011-2021 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
44
// Created by: Denis Krjuchkov
55
// Created: 2011.10.07
66

@@ -46,6 +46,33 @@ public static void BuildFilter(IndexInfo index)
4646

4747
protected override Expression VisitBinary(BinaryExpression b)
4848
{
49+
if (b.Left.StripCasts().Type.StripNullable().IsEnum
50+
&& b.Right.StripCasts().NodeType == ExpressionType.Constant) {
51+
52+
var rawEnum = b.Left.StripCasts();
53+
var typeToCast = (rawEnum.Type.IsNullable())
54+
? rawEnum.Type.StripNullable().GetEnumUnderlyingType().ToNullable()
55+
: rawEnum.Type.GetEnumUnderlyingType();
56+
57+
return base.VisitBinary(Expression.MakeBinary(
58+
b.NodeType,
59+
Expression.Convert(rawEnum, typeToCast),
60+
Expression.Convert(b.Right, typeToCast)));
61+
}
62+
else if (b.Right.StripCasts().Type.StripNullable().IsEnum
63+
&& b.Left.StripCasts().NodeType == ExpressionType.Constant) {
64+
65+
var rawEnum = b.Right.StripCasts();
66+
var typeToCast = (rawEnum.Type.IsNullable())
67+
? rawEnum.Type.StripNullable().GetEnumUnderlyingType().ToNullable()
68+
: rawEnum.Type.GetEnumUnderlyingType();
69+
70+
return base.VisitBinary(Expression.MakeBinary(
71+
b.NodeType,
72+
Expression.Convert(rawEnum, typeToCast),
73+
Expression.Convert(b.Left, typeToCast)));
74+
}
75+
4976
// Detect f!=null and f==null for entity fields
5077

5178
if (!b.NodeType.In(ExpressionType.Equal, ExpressionType.NotEqual))

0 commit comments

Comments
 (0)