Update exp to sql

This commit is contained in:
sunkaixuan 2023-10-13 19:29:34 +08:00
parent 55352ce26b
commit b4639c9892
3 changed files with 85 additions and 2 deletions

View File

@ -216,8 +216,26 @@ namespace SqlSugar
}
return this;
}
public ISugarQueryable<T> Includes<TReturn1>(Expression<Func<T, List<TReturn1>>> include1) { _Includes<T, TReturn1>(this.Context, include1); return this; }
public ISugarQueryable<T> Includes<TReturn1>(Expression<Func<T, TReturn1>> include1) { _Includes<T, TReturn1>(this.Context, include1); return this; }
public ISugarQueryable<T> Includes<TReturn1>(Expression<Func<T, List<TReturn1>>> include1)
{
var result = GetManyQueryable(include1);
if (result != null)
{
return result;
}
_Includes<T, TReturn1>(this.Context, include1);
return this;
}
public ISugarQueryable<T> Includes<TReturn1>(Expression<Func<T, TReturn1>> include1)
{
var result=GetManyQueryable(include1);
if (result != null)
{
return result;
}
_Includes<T, TReturn1>(this.Context, include1);
return this;
}
public ISugarQueryable<T> Includes<TReturn1, TReturn2>(Expression<Func<T, TReturn1>> include1, Expression<Func<TReturn1, List<TReturn2>>> include2) { _Includes<T, TReturn1, TReturn2>(this.Context, include1, include2); return this; }
public ISugarQueryable<T> Includes<TReturn1, TReturn2>(Expression<Func<T, TReturn1>> include1, Expression<Func<TReturn1, TReturn2>> include2) { _Includes<T, TReturn1, TReturn2>(this.Context, include1, include2); return this; }
public ISugarQueryable<T> Includes<TReturn1, TReturn2>(Expression<Func<T, List<TReturn1>>> include1, Expression<Func<TReturn1, List<TReturn2>>> include2) { _Includes<T, TReturn1, TReturn2>(this.Context, include1, include2); return this; }

View File

@ -107,6 +107,54 @@ namespace SqlSugar
result.QueryBuilder = clone.QueryBuilder;
return result;
}
private ISugarQueryable<T> GetManyQueryable<TReturn1>(Expression<Func<T, TReturn1>> include1)
{
ISugarQueryable<T> result = null;
var isManyMembers = IsMembers(include1);
if (isManyMembers)
{
var array = ExpressionTool.ExtractMemberNames(include1);
if (array.Count > 1)
{
if (array.Count == 2)
{
result = this.IncludesByNameString(array[0], array[1]);
}
else if (array.Count == 3)
{
result = this.IncludesByNameString(array[0], array[1], array[2]);
}
else if (array.Count == 4)
{
result = this.IncludesByNameString(array[0], array[1], array[2], array[3]);
}
else if (array.Count == 5)
{
result = this.IncludesByNameString(array[0], array[1], array[2], array[3], array[4]);
}
else if (array.Count == 6)
{
throw new Exception("Multiple levels of expression exceeded the upper limit");
}
}
}
return result;
}
private static bool IsMembers<TReturn1>(Expression<Func<T, TReturn1>> include1)
{
var isManyMembers = false;
var x = ((include1 as LambdaExpression).Body as MemberExpression)?.Expression;
if (x is MemberExpression)
{
var exp = (x as MemberExpression)?.Expression;
if (exp != null)
{
isManyMembers = true;
}
}
return isManyMembers;
}
}
public partial class NavQueryableProvider<T> : QueryableProvider<T>, NavISugarQueryable<T>
@ -244,5 +292,7 @@ namespace SqlSugar
if (this.QueryBuilder.Includes == null) this.QueryBuilder.Includes = new List<object>();
this.QueryBuilder.Includes.Add(navigat);
}
}
}

View File

@ -10,6 +10,21 @@ namespace SqlSugar
{
public class ExpressionTool
{
public static List<string> ExtractMemberNames(Expression expression)
{
var memberNames = new List<string>();
var currentExpression = (expression as LambdaExpression).Body;
while (currentExpression != null && currentExpression.NodeType == ExpressionType.MemberAccess)
{
var memberExpression = (MemberExpression)currentExpression;
memberNames.Add(memberExpression.Member.Name);
currentExpression = memberExpression.Expression;
}
memberNames.Reverse(); // Reverse the list to get the correct order of member names
return memberNames;
}
public static Expression<Func<T, bool>> ChangeLambdaExpression<T>(Expression<Func<T,bool>> exp,string replaceParameterName, string newParameterName)
{
var parameter = Expression.Parameter(typeof(T), newParameterName);