mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	- 优化 表达式中不能使用 c# 函数的问题,
> 如:where(a => HttpContext.Session.GetString("UserID") == a.UserId)
			
			
This commit is contained in:
		@@ -322,10 +322,12 @@ namespace FreeSql.Tests
 | 
				
			|||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
        public void Test1()
 | 
					        public void Test1()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            g.sqlite.Aop.ParseExpression += parseExp;
 | 
					            //g.sqlite.Aop.ParseExpression += parseExp;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var sqddddl = g.sqlite.Select<TaskBuild>().ToSql(t => t.OptionsEntity04 == "1".TryTo<int>());
 | 
					            var sqddddl = g.sqlite.Select<TaskBuild>().ToSql(t => t.OptionsEntity04 == "1".TryTo<int>());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            //var sqdddd2 = g.sqlite.Select<TaskBuild>().ToSql(t => t.OptionsEntity04 == t.NamespaceName.TryTo<int>());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var sqksdkfjl = g.sqlite.Select<TaskBuild>()
 | 
					            var sqksdkfjl = g.sqlite.Select<TaskBuild>()
 | 
				
			||||||
                .LeftJoin(a => a.Templates.Id2 == a.TemplatesId)
 | 
					                .LeftJoin(a => a.Templates.Id2 == a.TemplatesId)
 | 
				
			||||||
                .LeftJoin(a => a.Parent.Id == a.Id)
 | 
					                .LeftJoin(a => a.Parent.Id == a.Id)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -86,6 +86,13 @@ namespace System.Linq.Expressions
 | 
				
			|||||||
            var body = Expression.Not(exp.Body);
 | 
					            var body = Expression.Not(exp.Body);
 | 
				
			||||||
            return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
 | 
					            return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        internal static bool IsParameter(this Expression exp)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var test = new TextParameterExpressionVisitor();
 | 
				
			||||||
 | 
					            test.Visit(exp);
 | 
				
			||||||
 | 
					            return test.Result;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    internal class NewExpressionVisitor : ExpressionVisitor
 | 
					    internal class NewExpressionVisitor : ExpressionVisitor
 | 
				
			||||||
@@ -102,4 +109,15 @@ namespace System.Linq.Expressions
 | 
				
			|||||||
        protected override Expression VisitParameter(ParameterExpression node) =>
 | 
					        protected override Expression VisitParameter(ParameterExpression node) =>
 | 
				
			||||||
            node == _oldParameter ? this._newParameter : node;
 | 
					            node == _oldParameter ? this._newParameter : node;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    internal class TextParameterExpressionVisitor : ExpressionVisitor
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public bool Result { get; private set; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        protected override Expression VisitParameter(ParameterExpression node)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            if (!Result) Result = true;
 | 
				
			||||||
 | 
					            return node;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -837,6 +837,7 @@ namespace FreeSql.Internal
 | 
				
			|||||||
                    //}
 | 
					                    //}
 | 
				
			||||||
                    var other3Exp = ExpressionLambdaToSqlOther(exp3, tsc);
 | 
					                    var other3Exp = ExpressionLambdaToSqlOther(exp3, tsc);
 | 
				
			||||||
                    if (string.IsNullOrEmpty(other3Exp) == false) return other3Exp;
 | 
					                    if (string.IsNullOrEmpty(other3Exp) == false) return other3Exp;
 | 
				
			||||||
 | 
					                    if (exp3.IsParameter() == false) return formatSql(Expression.Lambda(exp3).Compile().DynamicInvoke(), tsc.mapType);
 | 
				
			||||||
                    throw new Exception($"未实现函数表达式 {exp3} 解析");
 | 
					                    throw new Exception($"未实现函数表达式 {exp3} 解析");
 | 
				
			||||||
                case ExpressionType.Parameter:
 | 
					                case ExpressionType.Parameter:
 | 
				
			||||||
                case ExpressionType.MemberAccess:
 | 
					                case ExpressionType.MemberAccess:
 | 
				
			||||||
@@ -1097,7 +1098,11 @@ namespace FreeSql.Internal
 | 
				
			|||||||
                case ExpressionType.Coalesce:
 | 
					                case ExpressionType.Coalesce:
 | 
				
			||||||
                    return _common.IsNull(ExpressionLambdaToSql(expBinary.Left, tsc), ExpressionLambdaToSql(expBinary.Right, tsc));
 | 
					                    return _common.IsNull(ExpressionLambdaToSql(expBinary.Left, tsc), ExpressionLambdaToSql(expBinary.Right, tsc));
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            if (dicExpressionOperator.TryGetValue(expBinary.NodeType, out var tryoper) == false) return "";
 | 
					            if (dicExpressionOperator.TryGetValue(expBinary.NodeType, out var tryoper) == false)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                if (exp.IsParameter() == false) return formatSql(Expression.Lambda(exp).Compile().DynamicInvoke(), tsc.mapType);
 | 
				
			||||||
 | 
					                return "";
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            return ExpressionBinary(tryoper, expBinary.Left, expBinary.Right, tsc);
 | 
					            return ExpressionBinary(tryoper, expBinary.Left, expBinary.Right, tsc);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user