Example #1
0
        public void UsingQueryBuilder()
        {
            // subject.
            var posts = new List <Post>()
            {
                new Post {
                    Id = 1, AuthorId = 1, Title = "Hello 1", Content = "World"
                },
                new Post {
                    Id = 2, AuthorId = 1, Title = "Hello 2", Content = "World"
                },
                new Post {
                    Id = 3, AuthorId = 2, Title = "Hello 3", Content = "World"
                },
            };

            // the query.
            var query        = posts.AsQueryable();
            var queryBuilder = new WhereBuilder(query);

            queryBuilder.Compare("AuthorId", ConditionOperators.Equal, 1);
            queryBuilder.And(subQuery =>
            {
                subQuery.Compare("Content", ConditionOperators.Equal, "World");
                subQuery.Or("Title", ConditionOperators.Contains, 3);
            });

            query = (IQueryable <Post>)queryBuilder.Build();
            Assert.AreEqual(2, query.Count());
        }
        protected virtual void ApplySimpleFilter <TSource>(WhereBuilder whereBuilder, ISimpleFilter filter)
        {
            var resolvedConditionOperator = ResolveConditionOperatorFrom(filter.Type);

            if (filter.CaseInsensitive == true)
            {
                filter.Path += ".ToLower()";
                filter.Value = $"{filter.Value}"?.ToLower();
            }

            whereBuilder.Compare(filter.Path, resolvedConditionOperator, filter.Value, and: filter.And == true, negate: filter.Not == true);
        }
        protected virtual void ApplySimpleFilter <TSource>(WhereBuilder whereBuilder, ISimpleFilter filter)
        {
            var resolvedConditionOperator = ResolveConditionOperatorFrom(filter.Type);

            whereBuilder.Compare(filter.Path, resolvedConditionOperator, filter.Value, and: filter.And == true, negate: filter.Not == true);
        }