Support Subquery.ToList()

This commit is contained in:
sunkaixuan 2022-12-01 23:21:48 +08:00
parent bb664be191
commit 960aec2695
7 changed files with 125 additions and 1 deletions

View File

@ -33,6 +33,7 @@ namespace SqlSugar
#endregion
#region Splicing basic
public Dictionary<string, object> SubToListParameters { get; set; }
public bool IsCrossQueryWithAttr { get; set; }
public Dictionary<string,string> CrossQueryItems { get; set; }
public bool IsSelectSingleFiledJson { get; set; }

View File

@ -135,6 +135,14 @@ namespace SqlSugar
var value = GetNewExpressionValue(item);
parameter.Context.Result.Append($" {value} AS {asName} ");
}
else if (IsSubToList(item))
{
var value = GetNewExpressionValue(item);
if (this.Context.SugarContext.QueryBuilder.SubToListParameters == null)
this.Context.SugarContext.QueryBuilder.SubToListParameters = new Dictionary<string, object>();
this.Context.SugarContext.QueryBuilder.SubToListParameters.Add(asName, value);
throw new Exception("子查询ToList开发中..");
}
else
{
asName = GetAsNameResolveAnObject(parameter, item, asName, isSameType);

View File

@ -12,6 +12,21 @@ namespace SqlSugar
public partial class BaseResolve
{
private static bool IsSubToList(Expression item)
{
return ExpressionTool.GetMethodName(item) == "ToList" && IsSubquery(item);
}
private static bool IsSubquery(Expression item)
{
var method = (item as MethodCallExpression);
if (method == null)
return false;
if (method.Object == null)
return false;
return method.Object.Type.Name.StartsWith("Subquery");
}
private bool IsExtSqlFuncObj(Expression item)
{
return this.Context.SqlFuncServices != null && item is MethodCallExpression && this.Context.SqlFuncServices.Any(it => it.UniqueMethodName == ExpressionTool.GetMethodName(item));

View File

@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class SubToList:ISubOperation
{
public bool HasWhere
{
get; set;
}
public string Name
{
get
{
return "ToList";
}
}
public Expression Expression
{
get; set;
}
public int Sort
{
get
{
return 200;
}
}
public ExpressionContext Context
{
get; set;
}
public string GetValue(Expression expression = null)
{;
var exp = expression as MethodCallExpression;
InitType(exp);
return "*,@SugarlistRowIndex as SugarlistRowIndex";
}
private void InitType(MethodCallExpression exp)
{
if (exp.Arguments.Count > 0)
{
foreach (var arg in (exp.Arguments[0] as LambdaExpression).Parameters)
{
if (this.Context.InitMappingInfo != null)
{
this.Context.InitMappingInfo(arg.Type);
this.Context.RefreshMapping();
}
}
}
}
public void SetShortName(MethodCallExpression exp, string result)
{
if (exp.Arguments[0] is LambdaExpression)
{
var parameters = (exp.Arguments[0] as LambdaExpression).Parameters;
if (parameters != null && parameters.Count > 0)
{
this.Context.CurrentShortName = this.Context.GetTranslationColumnName(parameters[0].ObjToString());
}
}
}
public void SetShortNameNext(MethodCallExpression exp, string result)
{
if (exp.Arguments.Count > 1 && exp.Arguments[1] is LambdaExpression)
{
var parameters = (exp.Arguments[1] as LambdaExpression).Parameters;
if (parameters != null && parameters.Count > 0)
{
this.Context.CurrentShortName = this.Context.GetTranslationColumnName(parameters[0].ObjToString());
}
}
}
}
}

View File

@ -37,7 +37,8 @@ namespace SqlSugar
new SubWithNolock(){ Context=Context },
new SubEnableTableFilter(){ Context=Context },
new SubSelectStringJoin{ Context=Context },
new SubDistinctCount{ Context=Context }
new SubDistinctCount{ Context=Context },
new SubToList{ Context=Context}
};
}

View File

@ -170,5 +170,10 @@ namespace SqlSugar
{
return this;
}
public List<T> ToList()
{
return new List<T>();
}
}
}

View File

@ -145,6 +145,7 @@
<Compile Include="ExpressionsToSql\ResolveItems\MethodCallExpressionResolve_BaseDateFomat.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubDistinctCount.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubSelectStringJoin.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubToList.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubWithNoLock.cs" />
<Compile Include="ExpressionsToSql\Subquery\Items\SubEnableTableFilter.cs" />
<Compile Include="Interface\ICustomConditionalFunc.cs" />