@@ -771,6 +771,32 @@ public string ExpressionLambdaToSql(Expression exp, ExpTSC tsc)
771771 }
772772 }
773773 }
774+ if ( new [ ] { "Where" , "WhereIf" } . Contains ( exp3tmpCall . Method . Name ) && exp3tmpCall . Object != null )
775+ {
776+ //这段特别兼容 DbSet.Where 表达式解析 #216
777+ var exp3tmpTestCall = Expression . Call ( exp3tmpCall . Object , exp3tmpCall . Method , exp3tmpCall . Arguments . Select ( a =>
778+ {
779+ var a2 = a ;
780+ if ( a2 . NodeType == ExpressionType . Quote ) a2 = ( a as UnaryExpression ) ? . Operand ;
781+ if ( a2 ? . NodeType == ExpressionType . Lambda )
782+ {
783+ var alambda = a2 as LambdaExpression ;
784+ if ( alambda . ReturnType == typeof ( bool ) )
785+ return Expression . Constant ( null , a . Type ) ; // Expression.Lambda(Expression.Constant(true), alambda.Parameters);
786+ }
787+ return a ;
788+ //if (a.Type == typeof(Expression<>).MakeGenericType(typeof(Func<,>).MakeGenericType(exp3tmp.Type.GetGenericArguments()[0], typeof(bool))))
789+ // return Expression.Lambda(Expression.Constant(true),
790+ } ) . ToArray ( ) ) ;
791+ fsql = Expression . Lambda ( exp3tmpTestCall ) . Compile ( ) . DynamicInvoke ( ) ;
792+ var fsqlFindMethod = fsql . GetType ( ) . GetMethod ( exp3tmpCall . Method . Name , exp3tmpCall . Arguments . Select ( a => a . Type ) . ToArray ( ) ) ;
793+ if ( fsqlFindMethod == null )
794+ throw new Exception ( $ "无法解析表达式方法 { exp3tmpCall . Method . Name } ") ;
795+ var exp3StackOld = exp3Stack ;
796+ exp3Stack = new Stack < Expression > ( ) ;
797+ exp3Stack . Push ( Expression . Call ( Expression . Constant ( fsql ) , fsqlFindMethod , exp3tmpCall . Arguments ) ) ;
798+ while ( exp3StackOld . Any ( ) ) exp3Stack . Push ( exp3StackOld . Pop ( ) ) ;
799+ }
774800 }
775801 if ( fsql == null ) fsql = Expression . Lambda ( exp3tmp ) . Compile ( ) . DynamicInvoke ( ) ;
776802 fsqlType = fsql ? . GetType ( ) ;
0 commit comments