Queryable.Select (dynamic exp)

This commit is contained in:
sunkaixuan
2023-09-29 14:56:34 +08:00
parent efc0753426
commit cc80aa5bc5
3 changed files with 32 additions and 1 deletions

View File

@@ -8,7 +8,7 @@ using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Dynamic;
using System.Threading.Tasks;
using System.Threading.Tasks;
namespace SqlSugar
{
@@ -1339,6 +1339,15 @@ namespace SqlSugar
}
return _Select<TResult>(expression);
}
public ISugarQueryable<TResult> Select<TResult>(string expShortName, FormattableString expSelect, Type propertyType)
{
var exp = DynamicCoreHelper.GetMember(typeof(TResult),propertyType, expShortName, expSelect);
return _Select<TResult>(exp);
}
public ISugarQueryable<T> Select(string expShortName, FormattableString expSelect,Type propertyType)
{
return Select<T>(expShortName, expSelect, propertyType);
}
public virtual ISugarQueryable<TResult> Select<TResult>(Expression<Func<T, TResult>> expression)
{
if (IsAppendNavColumns())

View File

@@ -151,6 +151,8 @@ namespace SqlSugar
Task<bool> AnyAsync(Expression<Func<T, bool>> expression, CancellationToken token);
bool Any();
Task<bool> AnyAsync();
ISugarQueryable<TResult> Select<TResult>(string expShortName, FormattableString expSelect, Type propertyType);
ISugarQueryable<T> Select(string expShortName, FormattableString expSelect, Type propertyType);
ISugarQueryable<TResult> Select<TResult>(Expression expression);
ISugarQueryable<TResult> Select<TResult>(Expression<Func<T, TResult>> expression);
ISugarQueryable<TResult> Select<TResult>(Expression<Func<T, TResult>> expression,bool isAutoFill);

View File

@@ -32,6 +32,26 @@ namespace SqlSugar
return lambda;
}
public static LambdaExpression GetMember(Type entityType,Type propertyType, string shortName, FormattableString memberSql)
{
var parameter = Expression.Parameter(entityType, "it");
// 提取 FormattableString 中的参数值
var arguments = memberSql.GetArguments();
var sql = ReplaceFormatParameters(memberSql.Format);
// 构建动态表达式,使用常量表达式和 whereSql 中的参数值
var lambda = SqlSugarDynamicExpressionParser.ParseLambda(
new[] { parameter },
propertyType,
sql,
memberSql.GetArguments()
);
return lambda;
}
private static string ReplaceFormatParameters(string format)
{
int parameterIndex = 0; // 起始参数索引