public void ExecuteDynamicQueryObject() { var source = new List<Product> { new Product {Name = "NPatterns", Version = 1.2}, new Product {Name = "NPatterns.Messaging.IoC",Version = 1.1}, new Product {Name = "NPatterns.ObjectRelational.EF",Version = 1.0}, new Product {Name = null,Version = 0} }; var query = new QueryObject(new DynamicQueryObjectExecutor()); query.Add(new Criteria { Field = "Name", Operator = CriteriaOperator.IsNotNull }); var criteriaGroup = new CriteriaGroup { Operator = CriteriaGroupOperator.Or }; criteriaGroup.Criterias.Add(new Criteria { Field = "Name", Operator = CriteriaOperator.IsEqualTo, Value = "npatterns" }); criteriaGroup.Criterias.Add(new Criteria { Field = "Name", Operator = CriteriaOperator.EndsWith, Value = "ioc" }); query.Add(criteriaGroup); query.Add(new SortDescription { Field = "Version", Direction = SortDirection.Ascending }); var result = query.Execute(source.AsQueryable()).ToList(); Assert.AreEqual(2, result.Count); Assert.IsTrue(result[0].Version < result[1].Version); query.Add(new Criteria { Field = "Version", Operator = CriteriaOperator.IsEqualTo, Value = 1.0 }, CriteriaGroupOperator.Or); var result2 = query.Execute(source.AsQueryable()).ToList(); Assert.AreEqual(3, result2.Count); }
public void ExecuteDynamicQueryObject() { var source = new List <Product> { new Product { Name = "NPatterns", Version = 1.2 }, new Product { Name = "NPatterns.Messaging.IoC", Version = 1.1 }, new Product { Name = "NPatterns.ObjectRelational.EF", Version = 1.0 }, new Product { Name = null, Version = 0 } }; var query = new QueryObject(new DynamicQueryObjectExecutor()); query.Add(new Criteria { Field = "Name", Operator = CriteriaOperator.IsNotNull }); var criteriaGroup = new CriteriaGroup { Operator = CriteriaGroupOperator.Or }; criteriaGroup.Criterias.Add(new Criteria { Field = "Name", Operator = CriteriaOperator.IsEqualTo, Value = "npatterns" }); criteriaGroup.Criterias.Add(new Criteria { Field = "Name", Operator = CriteriaOperator.EndsWith, Value = "ioc" }); query.Add(criteriaGroup); query.Add(new SortDescription { Field = "Version", Direction = SortDirection.Ascending }); var result = query.Execute(source.AsQueryable()).ToList(); Assert.AreEqual(2, result.Count); Assert.IsTrue(result[0].Version < result[1].Version); query.Add(new Criteria { Field = "Version", Operator = CriteriaOperator.IsEqualTo, Value = 1.0 }, CriteriaGroupOperator.Or); var result2 = query.Execute(source.AsQueryable()).ToList(); Assert.AreEqual(3, result2.Count); }
public override void Handle(UnaryExpression expression, VisitorContext context) { context.Visit(expression.Operand); var val = context.GetResult(); var result = new QueryObject(); result.Add("$not", val); context.SetResult(result); }
public void ExecuteDynamicQueryObjectWithComplexType() { var source = new List<ProductFeature> { new ProductFeature { Product = new Product {Name = "NPatterns", Version = 1.2}, Feature = new Feature {Name = "IUnitOfWork"} }, new ProductFeature { Product = new Product {Name = "NPatterns.Messaging.IoC", Version = 1.1}, Feature = new Feature {Name = "MessageBus"} }, new ProductFeature { Product = new Product {Name = "NPatterns.ObjectRelational.EF", Version = 1.0}, Feature = new Feature {Name = "UnitOfWork"} }, new ProductFeature { Product = new Product(), Feature = new Feature() } }; var query = new QueryObject(new DynamicQueryObjectExecutor()); query.Add(new Criteria { Field = "Product.Name", Operator = CriteriaOperator.IsNotNull }); var criteriaGroup = new CriteriaGroup { Operator = CriteriaGroupOperator.Or }; criteriaGroup.Criterias.Add(new Criteria { Field = "Product.Name", Operator = CriteriaOperator.IsEqualTo, Value = "npatterns" }); criteriaGroup.Criterias.Add(new Criteria { Field = "Product.Name", Operator = CriteriaOperator.EndsWith, Value = "ioc" }); query.Add(criteriaGroup); var result = query.Execute(source.AsQueryable()).ToList(); Assert.AreEqual(2, result.Count); query.Add(new Criteria { Field = "Product.Version", Operator = CriteriaOperator.IsEqualTo, Value = 1.0 }, CriteriaGroupOperator.Or); var result2 = query.Execute(source.AsQueryable()).ToList(); Assert.AreEqual(3, result2.Count); }
/// <summary> /// convert to QueryObject /// </summary> /// <param name="includeOnlyPrefix">prefix of the fields that we want to include in the QueryObject</param> /// <param name="excludePrefixes">prefixes of the fields that we want to exclude from the QueryObject</param> /// <returns></returns> public QueryObject ToQuery(string includeOnlyPrefix = null, string[] excludePrefixes = null) { var q = new QueryObject(new DynamicQueryObjectExecutor()); if (!string.IsNullOrWhiteSpace(Filters)) { var criteriaGroup = JsonConvert.DeserializeObject <CriteriaGroup>(Filters, new StringEnumConverter()); if (criteriaGroup != null && criteriaGroup.Valid) { if (!string.IsNullOrEmpty(includeOnlyPrefix)) { //remove all which does not start with prefix criteriaGroup.Criterias.RemoveAll(rule => !rule.Field.StartsWith(includeOnlyPrefix)); //update remains to clean prefix criteriaGroup.Criterias.ForEach( rule => rule.Field = rule.Field.Remove(0, includeOnlyPrefix.Length)); } if (excludePrefixes != null && excludePrefixes.Length > 0) //remove all which does start with prefix { criteriaGroup.Criterias.RemoveAll( rule => excludePrefixes.Any(prefix => rule.Field.StartsWith(prefix))); } q.Add(criteriaGroup); } } if (!string.IsNullOrEmpty(Sidx)) { string sortJsonString = string.Format("{{'sidx':'{0}','sord':'{1}'}}", Sidx, Sord); var d = JsonConvert.DeserializeObject <SortDescription>(sortJsonString, new StringEnumConverter()); q.Add(d); } return(q); }
public void ExecuteDynamicQueryObjectWithComplexType() { var source = new List <ProductFeature> { new ProductFeature { Product = new Product { Name = "NPatterns", Version = 1.2 }, Feature = new Feature { Name = "IUnitOfWork" } }, new ProductFeature { Product = new Product { Name = "NPatterns.Messaging.IoC", Version = 1.1 }, Feature = new Feature { Name = "MessageBus" } }, new ProductFeature { Product = new Product { Name = "NPatterns.ObjectRelational.EF", Version = 1.0 }, Feature = new Feature { Name = "UnitOfWork" } }, new ProductFeature { Product = new Product(), Feature = new Feature() } }; var query = new QueryObject(new DynamicQueryObjectExecutor()); query.Add(new Criteria { Field = "Product.Name", Operator = CriteriaOperator.IsNotNull }); var criteriaGroup = new CriteriaGroup { Operator = CriteriaGroupOperator.Or }; criteriaGroup.Criterias.Add(new Criteria { Field = "Product.Name", Operator = CriteriaOperator.IsEqualTo, Value = "npatterns" }); criteriaGroup.Criterias.Add(new Criteria { Field = "Product.Name", Operator = CriteriaOperator.EndsWith, Value = "ioc" }); query.Add(criteriaGroup); var result = query.Execute(source.AsQueryable()).ToList(); Assert.AreEqual(2, result.Count); query.Add(new Criteria { Field = "Product.Version", Operator = CriteriaOperator.IsEqualTo, Value = 1.0 }, CriteriaGroupOperator.Or); var result2 = query.Execute(source.AsQueryable()).ToList(); Assert.AreEqual(3, result2.Count); }