From 1a5bcef1fa4d2d90c650c1ceb3fef26791df4941 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 13 Jul 2022 17:51:47 +0800 Subject: [PATCH] Add Queryable.Filter(Type type) --- .../QueryableProvider/QueryableProvider.cs | 10 +++++- .../SqlBuilderProvider/QueryBuilder.cs | 32 +++++++++++++++++++ Src/Asp.Net/SqlSugar/Interface/IQueryable.cs | 1 + 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 87926d15d..853944f2e 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -193,7 +193,15 @@ namespace SqlSugar _Filter(FilterName, isDisabledGobalFilter); return this; } - + public ISugarQueryable Filter(Type type) + { + var whereString= QueryBuilder.GetFilters(type); + if (whereString.HasValue()) + { + this.Where(whereString); + } + return this; + } public virtual ISugarQueryable Mapper(Action mapperAction) { this.MapperAction=UtilMethods.IsNullReturnNew(this.MapperAction); diff --git a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs index da5deae71..546479c7d 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs +++ b/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; diff --git a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs index 2acdbc538..ccea0e449 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IQueryable.cs @@ -27,6 +27,7 @@ namespace SqlSugar ISugarQueryable InnerJoin(Expression> joinExpression); ISugarQueryable RightJoin(Expression> joinExpression); ISugarQueryable Filter(string FilterName, bool isDisabledGobalFilter = false); + ISugarQueryable Filter(Type type); ISugarQueryable Mapper(Action mapperAction); ISugarQueryable Mapper(Expression> expression); ISugarQueryable Mapper(Action> mapperAction);