@@ -1050,51 +1050,52 @@ private IMongoQuery BuildStringLengthQuery(Expression variableExpression, Expres
1050
1050
private IMongoQuery BuildStringCaseComparisonQuery ( Expression variableExpression , ExpressionType operatorType , ConstantExpression constantExpression )
1051
1051
{
1052
1052
var methodExpression = variableExpression as MethodCallExpression ;
1053
- if ( methodExpression ! = null )
1053
+ if ( methodExpression = = null )
1054
1054
{
1055
- var sourceExpression = methodExpression . Object as MemberExpression ;
1055
+ return null ;
1056
+ }
1057
+ var sourceExpression = methodExpression . Object as MemberExpression ;
1056
1058
1057
- if ( methodExpression . Type == typeof ( string ) && sourceExpression != null )
1058
- {
1059
- var serializationInfo = _serializationInfoHelper . GetSerializationInfo ( methodExpression . Object ) ;
1060
- var serializedValue = _serializationInfoHelper . SerializeValue ( serializationInfo , constantExpression . Value ) ;
1061
- var coalescedStringValue = constantExpression . Value == null ? string . Empty : serializedValue . AsString ;
1059
+ if ( methodExpression . Type != typeof ( string ) || sourceExpression == null )
1060
+ {
1061
+ return null ;
1062
+ }
1062
1063
1063
- string regexPattern = "/^" + Regex . Escape ( coalescedStringValue ) + "$/i" ;
1064
- var regex = new BsonRegularExpression ( regexPattern ) ;
1064
+ var serializationInfo = _serializationInfoHelper . GetSerializationInfo ( methodExpression . Object ) ;
1065
+ var serializedValue = _serializationInfoHelper . SerializeValue ( serializationInfo , constantExpression . Value ) ;
1066
+ var coalescedStringValue = constantExpression . Value == null ? string . Empty : serializedValue . AsString ;
1065
1067
1066
- bool caseMismatch = false ;
1068
+ string regexPattern = "/^" + Regex . Escape ( coalescedStringValue ) + "$/i" ;
1069
+ var regex = new BsonRegularExpression ( regexPattern ) ;
1067
1070
1068
- if ( methodExpression . Method . Name == "ToLower" && ( coalescedStringValue != coalescedStringValue . ToLower ( ) ) )
1069
- caseMismatch = true ;
1070
- else if ( methodExpression . Method . Name == "ToUpper" && ( coalescedStringValue != coalescedStringValue . ToUpper ( ) ) )
1071
- caseMismatch = true ;
1072
- else if ( constantExpression . Value == null )
1073
- caseMismatch = true ;
1071
+ bool caseMismatch = false ;
1074
1072
1075
- if ( operatorType == ExpressionType . Equal )
1076
- {
1077
- // if comparing Foo.ToLower() == "Some Non Lower Case String"
1078
- // then that is always false for all documents
1079
- if ( caseMismatch )
1080
- return Query . Exists ( "_id" , false ) ;
1073
+ if ( methodExpression . Method . Name == "ToLower" && ( coalescedStringValue != coalescedStringValue . ToLower ( ) ) )
1074
+ caseMismatch = true ;
1075
+ else if ( methodExpression . Method . Name == "ToUpper" && ( coalescedStringValue != coalescedStringValue . ToUpper ( ) ) )
1076
+ caseMismatch = true ;
1077
+ else if ( constantExpression . Value == null )
1078
+ caseMismatch = true ;
1081
1079
1082
- return Query . And ( Query . Exists ( serializationInfo . ElementName , true ) ,
1083
- Query . Matches ( serializationInfo . ElementName , regex ) ) ;
1084
- }
1085
- else if ( operatorType == ExpressionType . NotEqual )
1086
- {
1087
- // if comparing Foo.ToLower() != "Some Non Lower Case String"
1088
- // then that is always true as long as Foo is set/exists
1089
- if ( caseMismatch )
1090
- return Query . Exists ( serializationInfo . ElementName , true ) ;
1080
+ if ( operatorType == ExpressionType . Equal )
1081
+ {
1082
+ // if comparing Foo.ToLower() == "Some Non Lower Case String"
1083
+ // then that is always false for all documents
1084
+ if ( caseMismatch )
1085
+ return Query . Exists ( "_id" , false ) ;
1091
1086
1092
- return Query . And ( Query . Exists ( serializationInfo . ElementName , true ) ,
1093
- Query . Not ( serializationInfo . ElementName ) . Matches ( regex ) ) ;
1094
- }
1095
- else
1096
- return null ;
1097
- }
1087
+ return Query . And ( Query . Exists ( serializationInfo . ElementName , true ) ,
1088
+ Query . Matches ( serializationInfo . ElementName , regex ) ) ;
1089
+ }
1090
+ else if ( operatorType == ExpressionType . NotEqual )
1091
+ {
1092
+ // if comparing Foo.ToLower() != "Some Non Lower Case String"
1093
+ // then that is always true as long as Foo is set/exists
1094
+ if ( caseMismatch )
1095
+ return Query . Exists ( serializationInfo . ElementName , true ) ;
1096
+
1097
+ return Query . And ( Query . Exists ( serializationInfo . ElementName , true ) ,
1098
+ Query . Not ( serializationInfo . ElementName ) . Matches ( regex ) ) ;
1098
1099
}
1099
1100
1100
1101
return null ;
0 commit comments