Add Select(string expShortName, List<string> columns, params object[] args);

This commit is contained in:
sunkaixuan 2025-04-03 15:02:01 +08:00
parent b9e09e0c91
commit 1fc20c43fc
5 changed files with 108 additions and 1 deletions

View File

@ -1452,6 +1452,20 @@ namespace SqlSugar
{
return Select<T>(expShortName, expSelect, resultType);
}
public DynamicCoreSelectModel Select(string expShortName, List<string> columns,params object[] args)
{
DynamicCoreSelectModel dynamicCoreSelectModel = new DynamicCoreSelectModel();
var selectObj = DynamicCoreHelper.BuildPropertySelector(
expShortName, typeof(T),
columns,
args);
var exp = DynamicCoreHelper.GetMember(typeof(T), selectObj.ResultNewType, expShortName, selectObj.formattableString);
var method = GetType().GetMethod("_Select", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
.MakeGenericMethod(selectObj.ResultNewType);
dynamicCoreSelectModel.Value= method.Invoke(this, new object[] { exp });
return dynamicCoreSelectModel;
}
public virtual ISugarQueryable<TResult> Select<TResult>(Expression<Func<T, TResult>> expression)
{
if (IsAppendNavColumns())

View File

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SqlSugar
{
public class DynamicCoreSelectModel
{
public object Value { get; set; }
public object ToList()
{
if (Value is null)
{
throw new InvalidOperationException("Value cannot be null.");
}
var method = Value.GetType().GetMyMethod("ToList", 0);
if (method == null)
{
throw new InvalidOperationException("The Value object does not have a ToList method with no parameters.");
}
return method.Invoke(Value, null);
}
public object ToPageList(int pageNumber, int pageSize)
{
if (Value is null)
{
throw new InvalidOperationException("Value cannot be null.");
}
var method = Value.GetType().GetMyMethod("ToPageList", 2);
if (method == null)
{
throw new InvalidOperationException("The Value object does not have a ToPageList method with two parameters.");
}
return method.Invoke(Value, new object[] { pageNumber, pageSize });
}
public object ToPageList(int pageNumber, int pageSize, ref int totalNumber)
{
if (Value is null)
{
throw new InvalidOperationException("Value cannot be null.");
}
var method = Value.GetType().GetMyMethod("ToPageList", 3);
if (method == null)
{
throw new InvalidOperationException("The Value object does not have a ToPageList method with three parameters.");
}
var parameters = new object[] { pageNumber, pageSize, totalNumber };
var result = method.Invoke(Value, parameters);
totalNumber = (int)parameters[2];
return result;
}
}
}

View File

@ -262,7 +262,8 @@ namespace SqlSugar
using (reader)
{
var tType = typeof(T);
var classProperties = tType.GetProperties().ToList();
var classProperties = tType.GetProperties()
.Where(p => p.GetIndexParameters().Length == 0).ToList();
var reval = new List<T>();
if (reader != null && !reader.IsClosed)
{

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
@ -9,6 +10,33 @@ namespace SqlSugar
{
public class DynamicCoreHelper
{
public static BuildPropertySelectorResult BuildPropertySelector(string shortName, Type type, List<string> propertyNames, params object[] args)
{
BuildPropertySelectorResult result = new BuildPropertySelectorResult();
if (type == null)
throw new ArgumentNullException(nameof(type));
if (propertyNames == null || propertyNames.Count == 0)
throw new ArgumentNullException(nameof(propertyNames));
var parameter = Expression.Parameter(type, shortName);
// 解析多个属性,生成匿名类型
var newAnonymousTypeStr = $"new {{ {string.Join(", ", propertyNames)} }}";
newAnonymousTypeStr = ReplaceFormatParameters(newAnonymousTypeStr);
result.formattableString = FormattableStringFactory.Create(newAnonymousTypeStr, args);
var lambda = SqlSugarDynamicExpressionParser.ParseLambda(new[] { parameter }, null, newAnonymousTypeStr, args);
result.ResultNewType = lambda.Body.Type;
result.ShortName = shortName;
return result;
}
public class BuildPropertySelectorResult
{
public FormattableString formattableString { get; set; }
public string ShortName { get; set; }
public Type ResultNewType { get; set; }
}
public static Expression<Func<T, bool>> GetWhere<T>(string shortName, FormattableString whereSql)
{
return (Expression<Func<T, bool>>)GetWhere(typeof(T), shortName, whereSql);

View File

@ -9,6 +9,7 @@ namespace SqlSugar
ISugarQueryable<T> Having(IFuncModel model);
ISugarQueryable<T> OrderBy(List<OrderByModel> models);
ISugarQueryable<T> GroupBy(List<GroupByModel> models);
DynamicCoreSelectModel Select(string expShortName, List<string> columns, params object[] args);
ISugarQueryable<T> Select(List<SelectModel> models);
ISugarQueryable<TResult> Select<TResult>(List<SelectModel> models);
ISugarQueryable<T> Select(List<SelectModel> models,AsNameFormatType type);