Add Queryable.Filter(Type type)

This commit is contained in:
sunkaixuan 2022-07-13 17:51:47 +08:00
parent ac7df08ad5
commit 1a5bcef1fa
3 changed files with 42 additions and 1 deletions

View File

@ -193,7 +193,15 @@ namespace SqlSugar
_Filter(FilterName, isDisabledGobalFilter);
return this;
}
public ISugarQueryable<T> Filter(Type type)
{
var whereString= QueryBuilder.GetFilters(type);
if (whereString.HasValue())
{
this.Where(whereString);
}
return this;
}
public virtual ISugarQueryable<T> Mapper(Action<T> mapperAction)
{
this.MapperAction=UtilMethods.IsNullReturnNew(this.MapperAction);

View File

@ -273,6 +273,38 @@ namespace SqlSugar
}
return result;
}
internal string GetFilters(Type type)
{
var result = "";
if (this.Context != null)
{
var db = Context;
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
var index = 0;
if (db.QueryFilter.GeFilterList != null)
{
foreach (var item in db.QueryFilter.GeFilterList)
{
PropertyInfo field = item.GetType().GetProperty("exp", flag);
if (field != null)
{
Type ChildType = item.GetType().GetProperty("type", flag).GetValue(item, null) as Type;
if (ChildType == type)
{
var entityInfo = db.EntityMaintenance.GetEntityInfo(ChildType);
var exp = field.GetValue(item, null) as Expression;
var whereStr = index==0 ? " " : " AND ";
index++;
result += (whereStr + GetExpressionValue(exp, ResolveExpressType.WhereSingle).GetString());
}
}
}
}
}
return result;
}
public virtual string ToSqlString()
{
string oldOrderBy = this.OrderByValue;

View File

@ -27,6 +27,7 @@ namespace SqlSugar
ISugarQueryable<T, T2> InnerJoin<T2>(Expression<Func<T, T2, bool>> joinExpression);
ISugarQueryable<T, T2> RightJoin<T2>(Expression<Func<T, T2, bool>> joinExpression);
ISugarQueryable<T> Filter(string FilterName, bool isDisabledGobalFilter = false);
ISugarQueryable<T> Filter(Type type);
ISugarQueryable<T> Mapper(Action<T> mapperAction);
ISugarQueryable<T> Mapper<AType, BType, MappingType>(Expression<Func<MappingType, ManyToMany>> expression);
ISugarQueryable<T> Mapper(Action<T, MapperCache<T>> mapperAction);