public void ShouldFilterByIsNotCondition() { FilterCondition condition = new FilterCondition { Field = new ConversationField { Name = "Department Assignee", DataType = FieldDataType.Option }, MatchType = ConditionMatchType.IsNot, Value = "@My Department" }; var options = new ExpressionBuildOptions { UserId = 1, MyDepartments = new int[] { 1 } }; var expression = new DepartmentAssigneeExpression().SetOptions(options).Build(condition); var conditions = new List <Conversation> { new Conversation { Id = 1, DepartmentId = 1 }, new Conversation { Id = 2, DepartmentId = null } }.AsQueryable(); var result = conditions.Where(expression).ToList(); Assert.Equal(1, result.Count); Assert.Equal(2, result.First().Id); }
public Expression <Func <Conversation, bool> > Create(Filter filter, ExpressionBuildOptions options) { var expressions = filter.Conditions.Select(t => GetConditionExpression(t, options)).Where(t => t != null).ToList(); if (expressions.Count() == 0) { return(t => true); } if (filter.Type == FilterType.All) { var predicate = PredicateBuilder.New <Conversation>(); foreach (var expression in expressions) { predicate = predicate.And(expression); } return(predicate); } if (filter.Type == FilterType.Any) { var predicate = PredicateBuilder.New <Conversation>(); foreach (var expression in expressions) { predicate = predicate.Or(expression); } return(predicate); } if (filter.Type == FilterType.LogicalExpression) { var predicate = PredicateBuilder.New <Conversation>(); var expressionDic = filter.Conditions.ToDictionary(t => t.Index, t => GetConditionExpression(t, options)); var buildResult = LogicalExpressionBuilder.Build(expressionDic, filter.LogicalExpression); if (buildResult.IsSuccess) { return(predicate.And(buildResult.Expression)); } } return(t => true); }
private Expression <Func <Conversation, bool> > GetConditionExpression(FilterCondition condition, ExpressionBuildOptions options) { foreach (var expression in _conditionExpressions) { if (expression.IsMatch(condition)) { return(expression.SetOptions(options).Build(condition)); } } return(null); }