IEnumerable <T> IQueryFilterEngine <T> .SortCollection(IEnumerable <T> collection, QueryGroup queryGroup) { ConditionChecker.Requires(collection != null, "Occurrences list cannot be null."); var querySorters = queryGroup.SortingRules; if (querySorters == null || querySorters.Count == 0) { return(collection); } var targetedType = typeof(T); IOrderedEnumerable <T> result = querySorters[0].SortingOperation == SortingOperation.Ascending ? collection.OrderBy(x => targetedType.GetPropertyValue(querySorters[0].Field, x)) : collection.OrderByDescending( x => targetedType.GetPropertyValue(querySorters[0].Field, x)); if (querySorters.Count > 1) { result = querySorters.Skip(1).Aggregate( (IOrderedEnumerable <T>)result, (current, thenBy) => thenBy.SortingOperation == SortingOperation.Ascending ? current.ThenBy(x => targetedType.GetPropertyValue(thenBy.Field, x)) : current.ThenByDescending(x => targetedType.GetPropertyValue(thenBy.Field, x))); } return(result); }
public QuerySorter(string field, SortingOperation sortingOperation) { ConditionChecker.Requires(string.IsNullOrEmpty(field) == false); this.Field = field; this.SortingOperation = sortingOperation; }
protected QueryField( string name, string type) { ConditionChecker.Requires(string.IsNullOrEmpty(name) == false); ConditionChecker.Requires(string.IsNullOrEmpty(type) == false); this.Name = name; this.Type = type; }
public QueryRule(string field, FieldOperation fieldOperation, object value, LogicalOperation logicalOperation) { ConditionChecker.Requires(string.IsNullOrEmpty(field) == false); ConditionChecker.Requires(value != null); Field = field; FieldOperation = fieldOperation; Value = value; LogicalOperation = logicalOperation; }
Predicate <T> IQueryCompiler <T> .CompileRule(QueryRule queryRule) { ConditionChecker.Requires(queryRule != null, "Query Rule cannot be null."); // When migrating the database, some fields of the old DTO might be missing. if (!typeof(T).HasProperty(queryRule.Field)) { return(null); } var targetedType = Expression.Parameter(typeof(T)); var leftMember = Expression.Property(targetedType, queryRule.Field); var propertyType = typeof(T).GetProperty(queryRule.Field)?.PropertyType; if (propertyType == null) { return(null); } Expression expression; // The given rule is binary(aka : equal, less than , gte, etc.) if (Enum.TryParse(Operations.Get(queryRule.FieldOperation), out ExpressionType binaryOperator)) { var queriedValue = GetQueriedValue(queryRule, propertyType); expression = Expression.MakeBinary(binaryOperator, leftMember, queriedValue); } else { // The given rule is a method(aka: Contains, Equals, etc. var queriedMethod = GetQueriedMethod(queryRule, propertyType); if (queriedMethod == null) { return(null); } var rightMember = GetRightMember(queryRule, queriedMethod); if (propertyType == typeof(string)) { var toLowerMethodInfo = typeof(string).GetMethod("ToLower", new Type[] { }); Expression leftMemberLowerCase = Expression.Call(leftMember, toLowerMethodInfo ?? throw new MissingMethodException()); Expression rightMemberLowerCase = Expression.Call(rightMember, toLowerMethodInfo ?? throw new MissingMethodException()); expression = Expression.Call(leftMemberLowerCase, queriedMethod, rightMemberLowerCase); } else { expression = Expression.Call(leftMember, queriedMethod, rightMember); } } return(Expression.Lambda <Predicate <T> >(expression, targetedType).Compile()); }
Predicate <T> IQueryBuilder <T> .BuildOrPredicate(Predicate <T> leftPredicate, Predicate <T> rightPredicate) { ConditionChecker.Requires(leftPredicate != null || rightPredicate != null, "At least one predicate must not be null."); if (leftPredicate == null) { return(rightPredicate); } if (rightPredicate == null) { return(leftPredicate); } return(item => leftPredicate(item) || rightPredicate(item)); }
Predicate <T> IQueryBuilder <T> .BuildGroupPredicate(QueryGroup queryGroup) { ConditionChecker.Requires(queryGroup != null, "Query group cannot be null."); if (!queryGroup.HasChildren) { return(((IQueryBuilder <T>) this).BuildRulesPredicate(queryGroup.Rules)); } Predicate <T> groupPredicate = null; if (queryGroup.Rules.Count > 0) { groupPredicate = ((IQueryBuilder <T>) this).BuildRulesPredicate(queryGroup.Rules); } var groupChildrenPredicate = ((IQueryBuilder <T>) this).BuildGroupPredicate(queryGroup.InnerGroups[0]); if (queryGroup.InnerGroups.Count > 1) { groupChildrenPredicate = queryGroup.InnerGroups.Aggregate( groupChildrenPredicate, (current, group) => { if (group.LogicalOperation == LogicalOperation.And) { return(((IQueryBuilder <T>) this).BuildAndPredicate(current, ((IQueryBuilder <T>) this).BuildGroupPredicate(@group))); } return(((IQueryBuilder <T>) this).BuildOrPredicate(current, ((IQueryBuilder <T>) this).BuildGroupPredicate(@group))); }); } if (groupPredicate == null) { return(item => groupChildrenPredicate(item)); } if (queryGroup.InnerGroups[0].LogicalOperation == LogicalOperation.And) { return(((IQueryBuilder <T>) this).BuildAndPredicate(groupPredicate, groupChildrenPredicate)); } return(((IQueryBuilder <T>) this).BuildOrPredicate(groupPredicate, groupChildrenPredicate)); }
Predicate <T> IQueryBuilder <T> .BuildGroupsPredicate(IList <QueryGroup> queryGroups) { ConditionChecker.Requires(queryGroups != null && queryGroups.Count > 0, "Query groups cannot be null or empty."); var groupsPredicate = ((IQueryBuilder <T>) this).BuildGroupPredicate(queryGroups[0]); if (queryGroups.Count > 1) { groupsPredicate = queryGroups.Aggregate( groupsPredicate, (current, group) => { if (group.LogicalOperation == LogicalOperation.And) { return(((IQueryBuilder <T>) this).BuildAndPredicate(current, ((IQueryBuilder <T>) this).BuildGroupPredicate(@group))); } return(((IQueryBuilder <T>) this).BuildOrPredicate(current, ((IQueryBuilder <T>) this).BuildGroupPredicate(@group))); }); } return(item => groupsPredicate(item)); }