public static ICustomQueryable <T> IncludeMultiple <T>(this ICustomQueryable <T> query, params Expression <Func <T, object> >[] includes) where T : class { if (includes != null) { query.Result = includes.Aggregate(query.Result, (current, include) => current.Include(include)); } return(query); }
public static async Task <ICustomList <T> > ToCustomListAsync <T>(this ICustomQueryable <T> query, Paging paging) where T : class { var result = await query.Result.ToListAsync(); return(new CustomList <T> { Result = result, TotalCount = query.TotalCount, PageIndex = query.PageIndex, PageSize = query.PageSize, PageCount = query.PageCount }); }
internal static List <WhereClause> GetCriterias(ICustomQueryable searchModel) { var type = searchModel.GetType(); var criterias = new List <WhereClause>(); // Iterate through all the methods of the class. foreach (var propertyInfo in type.GetProperties()) { bool isCollection = propertyInfo.IsPropertyACollection(); if (!isCollection && propertyInfo.IsPropertyObject(searchModel)) { continue; } var criteria = new WhereClause(); var attr = Attribute.GetCustomAttributes(propertyInfo).FirstOrDefault(); // Check for the AnimalType attribute. if (attr?.GetType() == typeof(QueryOperatorAttribute)) { var data = (QueryOperatorAttribute)attr; criteria.UpdateAttributeData(data); if (data.Operator != WhereOperator.Contains && isCollection) { throw new ArgumentException($"{propertyInfo.Name} - For array the only Operator available is Contains"); } } if (isCollection) { criteria.Operator = WhereOperator.Contains; } var customValue = propertyInfo.GetValue(searchModel, null); if (customValue == null) { continue; } criteria.UpdateValues(propertyInfo); criterias.Add(criteria); } return(criterias.OrderBy(o => o.UseOr).ToList()); }
internal static ExpressionParserCollection GetOperators <TEntity>(ICustomQueryable model) { var expressions = new ExpressionParserCollection(); var type = model.GetType(); expressions.ParameterExpression = Expression.Parameter(typeof(TEntity), "model"); foreach (var propertyInfo in type.GetProperties()) { var criteria = GetCriteria(model, propertyInfo); if (criteria == null) { continue; } if (!typeof(TEntity).HasProperty(criteria.FieldName) && !criteria.FieldName.Contains(".")) { continue; } dynamic propertyValue = expressions.ParameterExpression; foreach (var part in criteria.FieldName.Split('.')) { propertyValue = Expression.PropertyOrField(propertyValue, part); } var expressionData = new ExpressionParser(); expressionData.FieldToFilter = propertyValue; expressionData.FilterBy = GetClosureOverConstant(criteria.Property.GetValue(model, null), GetNonNullable(criteria.Property.PropertyType)); expressionData.Criteria = criteria; if (criteria.Property.GetValue(model, null) != null) { expressions.Add(expressionData); } } return(expressions); }
internal static WhereClause GetCriteria(ICustomQueryable model, PropertyInfo propertyInfo) { bool isCollection = propertyInfo.IsPropertyACollection(); if (!isCollection && propertyInfo.IsPropertyObject(model)) { return(null); } var criteria = new WhereClause(); var attr = Attribute.GetCustomAttributes(propertyInfo); // Check for the AnimalType attribute. if (attr.Any(a => a.GetType() == typeof(QueryOperatorAttribute))) { var data = (QueryOperatorAttribute)attr.First(a => a.GetType() == typeof(QueryOperatorAttribute)); criteria.UpdateAttributeData(data); if (data.Operator != WhereOperator.Contains && isCollection) { throw new ArgumentException($"{propertyInfo.Name} - For array the only Operator available is Contains"); } } if (isCollection) { criteria.Operator = WhereOperator.Contains; } var customValue = propertyInfo.GetValue(model, null); if (customValue == null) { return(null); } criteria.UpdateValues(propertyInfo); return(criteria); }
public async Task <IEnumerable <IDomainUser> > Search(ICustomQueryable search) { var users = await _userManager.Users.Apply(search).ToListAsync(); return(users); }
public Task <int> Count(ICustomQueryable search) { return(_userManager.Users.Filter(search).CountAsync()); }
public static IQueryable <TEntity> Filter <TEntity>(this IQueryable <TEntity> result, ICustomQueryable model) { if (model == null) { return(result); } Expression lastExpression = null; var operations = ExpressionFactory.GetOperators <TEntity>(model); foreach (var expression in operations.Ordered()) { if (!expression.Criteria.CaseSensitive) { expression.FieldToFilter = Expression.Call(expression.FieldToFilter, typeof(string).GetMethods() .First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0)); expression.FilterBy = Expression.Call(expression.FilterBy, typeof(string).GetMethods() .First(m => m.Name == "ToUpper" && m.GetParameters().Length == 0)); } var actualExpression = GetExpression <TEntity>(expression); if (expression.Criteria.UseNot) { actualExpression = Expression.Not(actualExpression); } if (lastExpression == null) { lastExpression = actualExpression; } else { if (expression.Criteria.UseOr) { lastExpression = Expression.Or(lastExpression, actualExpression); } else { lastExpression = Expression.And(lastExpression, actualExpression); } } } return(lastExpression == null ? result : result.Where(Expression.Lambda <Func <TEntity, bool> >(lastExpression, operations.ParameterExpression))); }
public static IQueryable <TEntity> Apply <TEntity>(this IQueryable <TEntity> result, ICustomQueryable model) { result = result.Filter(model); if (model is IQuerySort sort) { result = result.Sort(sort); } if (model is IQueryPaging pagination) { result = result.Paginate(pagination); } return(result); }
public static IQueryable <TEntity> Filter <TEntity>(this IQueryable <TEntity> result, ICustomQueryable model) { if (model == null) { return(result); } var lastExpression = result.FilterExpression(model); return(lastExpression == null ? result : result.Where(lastExpression)); }
public Task <int> Count(ICustomQueryable search) { return(DbSet.CountAsync()); }
public async Task <List <PersistedGrant> > Search(ICustomQueryable search) { var grants = await DbSet.Apply(search).ToListAsync(); return(grants.Select(s => s.ToModel()).ToList()); }