Last active
June 2, 2016 20:14
-
-
Save jakejscott/b42fc3fec9efe173f4e22cbbc41be76a to your computer and use it in GitHub Desktop.
PredicateBuilder + LINQ + Marten
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| using System.Linq.Expressions; | |
| namespace Sprightly | |
| { | |
| public List<Product> GetProducts(IQuerySession session, Collection collection) | |
| { | |
| List<Product> results; | |
| if (collection.Type == CollectionType.Manual) | |
| { | |
| results = session.LoadMany<Product>(collection.ProductIds.ToArray()).ToList(); | |
| } | |
| else | |
| { | |
| Expression<Func<Product, bool>> predicate = p => p.Title != null; | |
| if (collection.Rules.Any()) | |
| { | |
| foreach (var _ in collection.Rules) | |
| { | |
| var rule = _; | |
| if (rule.Column == CollectionRuleColumn.ProductPrice) | |
| { | |
| double value = Convert.ToDouble(rule.Value); | |
| switch (rule.Operator) | |
| { | |
| case CollectionRuleOperator.IsEqualTo: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.Price == value) | |
| : predicate.Or(x => x.Price == value); | |
| break; | |
| case CollectionRuleOperator.IsNotEqualTo: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.Price != value) | |
| : predicate.Or(x => x.Price != value); | |
| break; | |
| case CollectionRuleOperator.IsLessThan: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.Price < value) | |
| : predicate.Or(x => x.Price < value); | |
| break; | |
| case CollectionRuleOperator.IsGreaterThan: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.Price > value) | |
| : predicate.Or(x => x.Price > value); | |
| break; | |
| case CollectionRuleOperator.StartsWith: | |
| case CollectionRuleOperator.EndsWith: | |
| case CollectionRuleOperator.Contains: | |
| case CollectionRuleOperator.DoesNotContain: | |
| throw new NotSupportedException(); | |
| } | |
| } | |
| else if (rule.Column == CollectionRuleColumn.ProductSalePrice) | |
| { | |
| double value = Convert.ToDouble(rule.Value); | |
| switch (rule.Operator) | |
| { | |
| case CollectionRuleOperator.IsEqualTo: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.SalePrice == value) | |
| : predicate.Or(x => x.SalePrice == value); | |
| break; | |
| case CollectionRuleOperator.IsNotEqualTo: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.SalePrice != value) | |
| : predicate.Or(x => x.SalePrice != value); | |
| break; | |
| case CollectionRuleOperator.IsLessThan: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.SalePrice < value) | |
| : predicate.Or(x => x.SalePrice < value); | |
| break; | |
| case CollectionRuleOperator.IsGreaterThan: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.SalePrice > value) | |
| : predicate.Or(x => x.SalePrice > value); | |
| break; | |
| case CollectionRuleOperator.StartsWith: | |
| case CollectionRuleOperator.EndsWith: | |
| case CollectionRuleOperator.Contains: | |
| case CollectionRuleOperator.DoesNotContain: | |
| throw new NotSupportedException(); | |
| } | |
| } | |
| else if (rule.Column == CollectionRuleColumn.ProductTitle) | |
| { | |
| string value = Convert.ToString(rule.Value); | |
| switch (rule.Operator) | |
| { | |
| case CollectionRuleOperator.IsEqualTo: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.Title == value) | |
| : predicate.Or(x => x.Title == value); | |
| break; | |
| case CollectionRuleOperator.IsNotEqualTo: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.Title != value) | |
| : predicate.Or(x => x.Title != value); | |
| break; | |
| case CollectionRuleOperator.StartsWith: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.Title.StartsWith(value, StringComparison.InvariantCultureIgnoreCase)) | |
| : predicate.Or(x => x.Title.StartsWith(value, StringComparison.InvariantCultureIgnoreCase)); | |
| break; | |
| case CollectionRuleOperator.EndsWith: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.Title.EndsWith(value, StringComparison.InvariantCultureIgnoreCase)) | |
| : predicate.Or(x => x.Title.EndsWith(value, StringComparison.InvariantCultureIgnoreCase)); | |
| break; | |
| case CollectionRuleOperator.Contains: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.Title.Contains(value)) | |
| : predicate.Or(x => x.Title.Contains(value)); | |
| break; | |
| case CollectionRuleOperator.DoesNotContain: | |
| case CollectionRuleOperator.IsLessThan: | |
| case CollectionRuleOperator.IsGreaterThan: | |
| throw new NotSupportedException(); | |
| } | |
| } | |
| else if (rule.Column == CollectionRuleColumn.ProductVendor) | |
| { | |
| string value = Convert.ToString(rule.Value); | |
| switch (rule.Operator) | |
| { | |
| case CollectionRuleOperator.IsEqualTo: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.Vendor == value) | |
| : predicate.Or(x => x.Vendor == value); | |
| break; | |
| case CollectionRuleOperator.IsNotEqualTo: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.Vendor != value) | |
| : predicate.Or(x => x.Vendor != value); | |
| break; | |
| case CollectionRuleOperator.StartsWith: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.Vendor.StartsWith(value, StringComparison.InvariantCultureIgnoreCase)) | |
| : predicate.Or(x => x.Vendor.StartsWith(value, StringComparison.InvariantCultureIgnoreCase)); | |
| break; | |
| case CollectionRuleOperator.EndsWith: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.Vendor.EndsWith(value, StringComparison.InvariantCultureIgnoreCase)) | |
| : predicate.Or(x => x.Vendor.EndsWith(value, StringComparison.InvariantCultureIgnoreCase)); | |
| break; | |
| case CollectionRuleOperator.Contains: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.Vendor.Contains(value)) | |
| : predicate.Or(x => x.Vendor.Contains(value)); | |
| break; | |
| case CollectionRuleOperator.DoesNotContain: | |
| case CollectionRuleOperator.IsLessThan: | |
| case CollectionRuleOperator.IsGreaterThan: | |
| throw new NotSupportedException(); | |
| } | |
| } | |
| else if (rule.Column == CollectionRuleColumn.ProductType) | |
| { | |
| string value = Convert.ToString(rule.Value); | |
| switch (rule.Operator) | |
| { | |
| case CollectionRuleOperator.IsEqualTo: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.ProductType == value) | |
| : predicate.Or(x => x.ProductType == value); | |
| break; | |
| case CollectionRuleOperator.IsNotEqualTo: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.ProductType != value) | |
| : predicate.Or(x => x.ProductType != value); | |
| break; | |
| case CollectionRuleOperator.StartsWith: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.ProductType.StartsWith(value, StringComparison.InvariantCultureIgnoreCase)) | |
| : predicate.Or(x => x.ProductType.StartsWith(value, StringComparison.InvariantCultureIgnoreCase)); | |
| break; | |
| case CollectionRuleOperator.EndsWith: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.ProductType.EndsWith(value, StringComparison.InvariantCultureIgnoreCase)) | |
| : predicate.Or(x => x.ProductType.EndsWith(value, StringComparison.InvariantCultureIgnoreCase)); | |
| break; | |
| case CollectionRuleOperator.Contains: | |
| predicate = rule.MatchConditions == CollectionRuleMatchConditions.AllConditions | |
| ? predicate.And(x => x.ProductType.Contains(value)) | |
| : predicate.Or(x => x.ProductType.Contains(value)); | |
| break; | |
| case CollectionRuleOperator.DoesNotContain: | |
| case CollectionRuleOperator.IsLessThan: | |
| case CollectionRuleOperator.IsGreaterThan: | |
| throw new NotSupportedException(); | |
| } | |
| } | |
| } | |
| } | |
| results = session.Query<Product>().Where(predicate).ToList(); | |
| } | |
| return results; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| using System.Linq.Expressions; | |
| namespace Sprightly.Util | |
| { | |
| public static class PredicateBuilder | |
| { | |
| public static Expression<Func<T, bool>> True<T>() { return f => true; } | |
| public static Expression<Func<T, bool>> False<T>() { return f => false; } | |
| public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) | |
| { | |
| var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>()); | |
| return Expression.Lambda<Func<T, bool>>(Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters); | |
| } | |
| public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) | |
| { | |
| var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>()); | |
| return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment