mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-12-05 03:17:41 +08:00
Update exp to sql
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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>();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ namespace SqlSugar.Extensions
|
||||
///Common Extensions for external users
|
||||
/// </summary>
|
||||
public static class UtilExtensions
|
||||
{
|
||||
{
|
||||
public static int ObjToInt(this object thisValue)
|
||||
{
|
||||
int reval = 0;
|
||||
|
||||
Reference in New Issue
Block a user