@@ -278,7 +278,7 @@ public string[] ExpressionSelectColumns_MemberAccess_New_NewArrayInit(List<Selec
278278 } ;
279279 public string ExpressionWhereLambdaNoneForeignObject ( List < SelectTableInfo > _tables , TableInfo table , List < SelectColumnInfo > _selectColumnMap , Expression exp , Func < Expression [ ] , string > getSelectGroupingMapString ) {
280280 var sql = ExpressionLambdaToSql ( exp , new ExpTSC { _tables = _tables , _selectColumnMap = _selectColumnMap , getSelectGroupingMapString = getSelectGroupingMapString , tbtype = SelectTableInfoType . From , isQuoteName = true , isDisableDiyParse = false , style = ExpressionStyle . Where , currentTable = table } ) ;
281- if ( exp . NodeType == ExpressionType . MemberAccess && exp . Type == typeof ( bool ) )
281+ if ( exp . NodeType == ExpressionType . MemberAccess && exp . Type == typeof ( bool ) && sql . Contains ( " IS " ) == false && sql . Contains ( " = " ) == false )
282282 return $ "{ sql } = { formatSql ( true , null ) } ";
283283 switch ( sql ) {
284284 case "1" :
@@ -291,7 +291,7 @@ public string ExpressionWhereLambdaNoneForeignObject(List<SelectTableInfo> _tabl
291291
292292 public string ExpressionWhereLambda ( List < SelectTableInfo > _tables , Expression exp , Func < Expression [ ] , string > getSelectGroupingMapString ) {
293293 var sql = ExpressionLambdaToSql ( exp , new ExpTSC { _tables = _tables , getSelectGroupingMapString = getSelectGroupingMapString , tbtype = SelectTableInfoType . From , isQuoteName = true , isDisableDiyParse = false , style = ExpressionStyle . Where } ) ;
294- if ( exp . NodeType == ExpressionType . MemberAccess && exp . Type == typeof ( bool ) )
294+ if ( exp . NodeType == ExpressionType . MemberAccess && exp . Type == typeof ( bool ) && sql . Contains ( " IS " ) == false && sql . Contains ( " = " ) == false )
295295 return $ "{ sql } = { formatSql ( true , null ) } ";
296296 switch ( sql ) {
297297 case "1" :
@@ -303,26 +303,26 @@ public string ExpressionWhereLambda(List<SelectTableInfo> _tables, Expression ex
303303 }
304304 public void ExpressionJoinLambda ( List < SelectTableInfo > _tables , SelectTableInfoType tbtype , Expression exp , Func < Expression [ ] , string > getSelectGroupingMapString ) {
305305 var tbidx = _tables . Count ;
306- var filter = ExpressionLambdaToSql ( exp , new ExpTSC { _tables = _tables , getSelectGroupingMapString = getSelectGroupingMapString , tbtype = tbtype , isQuoteName = true , isDisableDiyParse = false , style = ExpressionStyle . Where } ) ;
307- if ( exp . NodeType == ExpressionType . MemberAccess && exp . Type == typeof ( bool ) )
308- filter = $ "{ filter } = { formatSql ( true , null ) } ";
309- switch ( filter ) {
306+ var sql = ExpressionLambdaToSql ( exp , new ExpTSC { _tables = _tables , getSelectGroupingMapString = getSelectGroupingMapString , tbtype = tbtype , isQuoteName = true , isDisableDiyParse = false , style = ExpressionStyle . Where } ) ;
307+ if ( exp . NodeType == ExpressionType . MemberAccess && exp . Type == typeof ( bool ) && sql . Contains ( " IS " ) == false && sql . Contains ( " = " ) == false )
308+ sql = $ "{ sql } = { formatSql ( true , null ) } ";
309+ switch ( sql ) {
310310 case "1" :
311- case "'t'" : filter = "1=1" ; break ;
311+ case "'t'" : sql = "1=1" ; break ;
312312 case "0" :
313- case "'f'" : filter = "1=2" ; break ;
313+ case "'f'" : sql = "1=2" ; break ;
314314 default : break ;
315315 }
316316 if ( _tables . Count > tbidx ) {
317317 _tables [ tbidx ] . Type = tbtype ;
318- _tables [ tbidx ] . On = filter ;
318+ _tables [ tbidx ] . On = sql ;
319319 for ( var a = tbidx + 1 ; a < _tables . Count ; a ++ )
320320 _tables [ a ] . Type = SelectTableInfoType . From ;
321321 } else {
322322 var find = _tables . Where ( ( a , c ) => c > 0 && ( a . Type == tbtype || a . Type == SelectTableInfoType . From ) && string . IsNullOrEmpty ( a . On ) ) . LastOrDefault ( ) ;
323323 if ( find != null ) {
324324 find . Type = tbtype ;
325- find . On = filter ;
325+ find . On = sql ;
326326 }
327327 }
328328 }
@@ -409,7 +409,14 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc) {
409409 switch ( exp . NodeType ) {
410410 case ExpressionType . Not :
411411 var notExp = ( exp as UnaryExpression ) ? . Operand ;
412- if ( notExp . NodeType == ExpressionType . MemberAccess ) return $ "{ ExpressionLambdaToSql ( notExp , tsc ) } = { formatSql ( false , null ) } ";
412+ if ( notExp . NodeType == ExpressionType . MemberAccess ) {
413+ var notBody = ExpressionLambdaToSql ( notExp , tsc ) ;
414+ if ( notBody . Contains ( " IS NULL" ) ) return notBody . Replace ( " IS NULL" , " IS NOT NULL" ) ;
415+ if ( notBody . Contains ( " IS NOT NULL" ) ) return notBody . Replace ( " IS NOT NULL" , " IS NULL" ) ;
416+ if ( notBody . Contains ( "=" ) ) return notBody . Replace ( "=" , "!=" ) ;
417+ if ( notBody . Contains ( "!=" ) ) return notBody . Replace ( "!=" , "=" ) ;
418+ return $ "{ notBody } = { formatSql ( false , null ) } ";
419+ }
413420 return $ "not({ ExpressionLambdaToSql ( notExp , tsc ) } )";
414421 case ExpressionType . Quote : return ExpressionLambdaToSql ( ( exp as UnaryExpression ) ? . Operand , tsc ) ;
415422 case ExpressionType . Lambda : return ExpressionLambdaToSql ( ( exp as LambdaExpression ) ? . Body , tsc ) ;
@@ -665,7 +672,8 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc) {
665672 case ExpressionType . MemberAccess :
666673 var exp4 = exp as MemberExpression ;
667674 if ( exp4 != null ) {
668- if ( exp4 . Expression != null && exp4 . Expression . Type . IsArray == false && exp4 . Expression . Type . IsNullableType ( ) ) return ExpressionLambdaToSql ( exp4 . Expression , tsc ) ;
675+ if ( exp4 . Expression != null && exp4 . Expression . Type . IsArray == false && exp4 . Expression . Type . IsNullableType ( ) )
676+ return exp4 . Member . Name == "HasValue" ? $ "{ ExpressionLambdaToSql ( exp4 . Expression , tsc ) } IS NOT NULL" : ExpressionLambdaToSql ( exp4 . Expression , tsc ) ;
669677 var extRet = "" ;
670678 var memberType = exp4 . Expression ? . Type ?? exp4 . Type ;
671679 switch ( memberType . FullName ) {
0 commit comments