Update exp to sql

This commit is contained in:
sunkaixuan
2023-10-05 18:42:12 +08:00
parent 473570f702
commit 06fb5c2cd5
5 changed files with 70 additions and 1 deletions

View File

@@ -1265,6 +1265,21 @@ namespace SqlSugar
#region Other
private bool IsSingleWithChildTableQuery()
{
return this.QueryBuilder.IsSingle() && this.QueryBuilder.TableShortName.HasValue();
}
private Expression<Func<T, bool>> ReplaceMasterTableParameters(Expression<Func<T, bool>> expression)
{
var parameterName = (expression as LambdaExpression)?.Parameters?.FirstOrDefault()?.Name;
if (parameterName != null && parameterName != this.QueryBuilder.TableShortName)
{
expression = ExpressionTool.ChangeLambdaExpression(expression, parameterName, this.QueryBuilder.TableShortName);
}
return expression;
}
private void orderPropertyNameByJoin(string orderPropertyName, OrderByType? orderByType)
{
var shortName = orderPropertyName.Split('.').FirstOrDefault();

View File

@@ -934,9 +934,14 @@ namespace SqlSugar
}
public virtual ISugarQueryable<T> Where(Expression<Func<T, bool>> expression)
{
if (IsSingleWithChildTableQuery())
{
expression = ReplaceMasterTableParameters(expression);
}
this._Where(expression);
return this;
}
public virtual ISugarQueryable<T> Where(string whereString, object whereObj = null)
{
if (whereString.HasValue())
@@ -1008,6 +1013,10 @@ namespace SqlSugar
public virtual ISugarQueryable<T> WhereIF(bool isWhere, Expression<Func<T, bool>> expression)
{
if (!isWhere) return this;
if (IsSingleWithChildTableQuery())
{
expression = ReplaceMasterTableParameters(expression);
}
_Where(expression);
return this;
}

View File

@@ -10,6 +10,27 @@ namespace SqlSugar
{
public class ExpressionTool
{
public static Expression<Func<T, bool>> ChangeLambdaExpression<T>(Expression<Func<T,bool>> exp,string replaceParameterName, string newParameterName)
{
var parameter = Expression.Parameter(typeof(T), newParameterName);
// 替换Lambda表达式中指定参数名
var visitor = new ParameterReplacer(replaceParameterName, parameter);
var newBody = visitor.Visit(exp);
return (Expression<Func<T, bool>>)newBody;
}
public static Expression ChangeLambdaExpression(Expression exp, Type targetType, string replaceParameterName, string newParameterName)
{
var parameter = Expression.Parameter(targetType, newParameterName);
// 替换Lambda表达式中指定参数名
var visitor = new ParameterReplacer(replaceParameterName, parameter);
var newBody = visitor.Visit(exp);
return newBody;
}
public static List<string> GetNewArrayMembers(NewArrayExpression newArrayExpression)
{
List<string> strings = new List<string>();

View File

@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
namespace SqlSugar
{
internal class ParameterReplacer : ExpressionVisitor
{
private readonly string _oldParameterName;
private readonly ParameterExpression _newParameter;
public ParameterReplacer(string oldParameterName, ParameterExpression newParameter)
{
_oldParameterName = oldParameterName;
_newParameter = newParameter;
}
protected override Expression VisitParameter(ParameterExpression node)
{
return node.Name == _oldParameterName ? _newParameter : base.VisitParameter(node);
}
}
}