@@ -250,9 +250,6 @@ private SqlExpression SimplifyNullNotNullExpression(
250
250
// in general:
251
251
// binaryOp(a, b) == null -> a == null || b == null
252
252
// binaryOp(a, b) != null -> a != null && b != null
253
- // for coalesce:
254
- // (a ?? b) == null -> a == null && b == null
255
- // (a ?? b) != null -> a != null || b != null
256
253
// for AndAlso, OrElse we can't do this optimization
257
254
// we could do something like this, but it seems too complicated:
258
255
// (a && b) == null -> a == null && b != 0 || a != 0 && b == null
@@ -262,15 +259,7 @@ private SqlExpression SimplifyNullNotNullExpression(
262
259
var newLeft = SimplifyNullNotNullExpression ( operatorType , sqlBinaryOperand . Left , typeof ( bool ) , typeMapping ) ;
263
260
var newRight = SimplifyNullNotNullExpression ( operatorType , sqlBinaryOperand . Right , typeof ( bool ) , typeMapping ) ;
264
261
265
- return sqlBinaryOperand . OperatorType == ExpressionType . Coalesce
266
- ? SimplifyLogicalSqlBinaryExpression (
267
- operatorType == ExpressionType . Equal
268
- ? ExpressionType . AndAlso
269
- : ExpressionType . OrElse ,
270
- newLeft ,
271
- newRight ,
272
- typeMapping )
273
- : SimplifyLogicalSqlBinaryExpression (
262
+ return SimplifyLogicalSqlBinaryExpression (
274
263
operatorType == ExpressionType . Equal
275
264
? ExpressionType . OrElse
276
265
: ExpressionType . AndAlso ,
@@ -279,6 +268,25 @@ private SqlExpression SimplifyNullNotNullExpression(
279
268
typeMapping ) ;
280
269
}
281
270
break ;
271
+
272
+ case SqlFunctionExpression sqlFunctionExpression
273
+ when sqlFunctionExpression . IsBuiltIn
274
+ && string . Equals ( "COALESCE" , sqlFunctionExpression . Name , StringComparison . OrdinalIgnoreCase ) :
275
+ // for coalesce:
276
+ // (a ?? b) == null -> a == null && b == null
277
+ // (a ?? b) != null -> a != null || b != null
278
+ var leftArgument = SimplifyNullNotNullExpression (
279
+ operatorType , sqlFunctionExpression . Arguments [ 0 ] , typeof ( bool ) , typeMapping ) ;
280
+ var rightArgument = SimplifyNullNotNullExpression (
281
+ operatorType , sqlFunctionExpression . Arguments [ 1 ] , typeof ( bool ) , typeMapping ) ;
282
+
283
+ return SimplifyLogicalSqlBinaryExpression (
284
+ operatorType == ExpressionType . Equal
285
+ ? ExpressionType . AndAlso
286
+ : ExpressionType . OrElse ,
287
+ leftArgument ,
288
+ rightArgument ,
289
+ typeMapping ) ;
282
290
}
283
291
break ;
284
292
}
0 commit comments