Update db.QueryableByObject

This commit is contained in:
sunkaixuan
2023-06-18 15:19:52 +08:00
parent b42f746d6f
commit def56dba69
3 changed files with 57 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
@@ -20,6 +21,55 @@ namespace SqlSugar
_Includes<T, TReturn1>(this.Context, include1);
return this;
}
public ISugarQueryable<T> IncludesByNameString(string navMemberName, string thenNavMemberName2)
{
var method = this.GetType().GetMethods().Where(it => it.Name == "IncludesByExpression2")
.First();
List<Expression> parametres = new List<Expression>();
List<Type> types = new List<Type>();
var entityInfo = this.EntityInfo;
method = GetIncludesByNameStringMethod(types,navMemberName, method, parametres, entityInfo);
//var navFirst = GetNavColumnInfo(navMemberName, entityInfo);
var entityInfo2 = this.Context.EntityMaintenance.GetEntityInfo(types.Last());
method = GetIncludesByNameStringMethod(types,thenNavMemberName2, method, parametres, entityInfo2);
method.MakeGenericMethod(types.ToArray()).Invoke(this, parametres.Cast<object>().ToArray());
return this;
}
private static MethodInfo GetIncludesByNameStringMethod(List<Type> types,string navMemberName, MethodInfo method, List<Expression> parametres, EntityInfo entityInfo)
{
var navFirst = GetNavColumnInfo(navMemberName, entityInfo);
parametres.AddRange(GetIncludesByNameStringParameters(entityInfo.Type, navFirst));
if (navFirst.PropertyInfo.PropertyType.FullName.IsCollectionsList())
{
types.Add(navFirst.PropertyInfo.PropertyType.GetGenericArguments()[0]);
}
else
{
types.Add(navFirst.PropertyInfo.PropertyType);
}
return method;
}
private static EntityColumnInfo GetNavColumnInfo(string navMemberName, EntityInfo entityInfo)
{
return entityInfo.Columns.Where(it => it.Navigat != null && it.PropertyName.EqualCase(navMemberName)).FirstOrDefault();
}
private static List<Expression> GetIncludesByNameStringParameters(Type type,EntityColumnInfo item)
{
var parametres = new List<Expression> { };
var properyType = item.PropertyInfo.PropertyType;
var properyItemType = properyType;
if (properyType.FullName.IsCollectionsList())
{
properyItemType = properyType.GetGenericArguments()[0];
}
var exp = ExpressionBuilderHelper.CreateExpressionSelectField(type, item.PropertyName, properyType);
parametres.Add(exp);
return parametres;
}
public ISugarQueryable<T> IncludesByNameString(string navMemberName)
{
var navs = this.EntityInfo.Columns.Where(it => it.Navigat != null&&it.PropertyName.EqualCase(navMemberName)).ToList();

View File

@@ -178,6 +178,12 @@ namespace SqlSugar
this.QueryableObj = method.Invoke(QueryableObj, new object[] { navProperyName });
return this;
}
public QueryMethodInfo Includes(string navProperyName,string thenNavProperyName2)
{
var method = QueryableObj.GetType().GetMyMethod("IncludesByNameString", 2, typeof(string),typeof(string));
this.QueryableObj = method.Invoke(QueryableObj, new object[] { navProperyName , thenNavProperyName2 });
return this;
}
#endregion
#region Result

View File

@@ -17,6 +17,7 @@ namespace SqlSugar
ISugarQueryable<T> IncludesByExpression2<TReturn1, TReturn2>(Expression include1, Expression include2);
ISugarQueryable<T> IncludesByExpression<TReturn1>(Expression include1);
ISugarQueryable<T> IncludesByNameString(string navMemberName);
ISugarQueryable<T> IncludesByNameString(string navMemberName,string thenNavMemberName2);
ISugarQueryable<T> IncludesAllFirstLayer(params string[] ignoreProperyNameList);
ISugarQueryable<T> IncludesAllSecondLayer<TReturn1>(Expression<Func<T, TReturn1>> expression,params string[] ignoreProperyNameList);
ISugarQueryable<T> Includes<TReturn1>(Expression<Func<T, List<TReturn1>>> include1);