Browse Source

Add Queryable.Filter(Type type)

pull/21/MERGE
sunkaixuan 2 years ago
parent
commit
1a5bcef1fa
  1. 10
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  2. 32
      Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs
  3. 1
      Src/Asp.Net/SqlSugar/Interface/IQueryable.cs

10
Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs

@ -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);

32
Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs

@ -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;

1
Src/Asp.Net/SqlSugar/Interface/IQueryable.cs

@ -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);

Loading…
Cancel
Save