static void test()
 {
     Expression <Func <Order, List <OrderLine> > >  selectOrderLines   = o => o.Lines;
     Expression <Func <List <OrderLine>, Boolean> > validateOrderLines = lines => lines.Count > 0;
     var validateOrder = ParameterToMemberExpressionRebinder.CombinePropertySelectorWithPredicate(selectOrderLines, validateOrderLines);
     // validateOrder: {o => (o.Lines.Count > 0)}
 }
Beispiel #2
0
        public void ShouldReflectivelyChangeCollectionParameterForLambdaIntoMemberAccessor()
        {
            var data = new List <ExampleLeaf>
            {
                new ExampleLeaf
                {
                    SecuredRoots = new[]
                    {
                        new ExampleRoot
                        {
                            Id = 1
                        }
                    }
                },
                new ExampleLeaf
                {
                    SecuredRoots = new[]
                    {
                        new ExampleRoot
                        {
                            Id = 4
                        }
                    }
                },
                new ExampleLeaf
                {
                    SecuredRoots = new[]
                    {
                        new ExampleRoot
                        {
                            Id = 5
                        }
                    }
                }
            };

            Expression <Func <ExampleLeaf, IEnumerable <ExampleRoot> > > selector = x => x.SecuredRoots;
            LambdaExpression selectorExpression = selector;
            List <long>      ids = new List <long> {
                1, 2, 3
            };
            Expression <Func <IEnumerable <ExampleRoot>, bool> > predicate = x => x.Any(c => ids.Contains(c.Id));
            LambdaExpression predicateExpression = predicate;

            // x => x.SecuredRoots.Any(c => ids.Contains(c.Id))

            var resultingCombined = ParameterToMemberExpressionRebinder.CombineSinglePropertySelectorWithPredicate(selectorExpression, predicateExpression);

            var resultingPredicate = (Func <ExampleLeaf, bool>)resultingCombined.Compile();

            var results = data.Where(resultingPredicate);

            results.Should().HaveCount(1);
        }
        public void CombineTwoObjectExpressions()
        {
            Expression <Func <Employee, object> > filter1 = p => p.Info;
            Expression <Func <Employee, object> > filter2 = p => p.Profile;

            // Trying to combine two object expressions fails
            Expression <Func <Employee, object> > filterCombined = ParameterToMemberExpressionRebinder.CombinePropertySelectorWithPredicate(filter1, filter2);


            filterCombined.Body.ToString().ShouldBe("((p => p.Info And p => p.Profile))"); //Something like this anyway...
        }
    public static Expression <Func <T, bool> > CombinePropertySelectorWithPredicate <T, T2>(
        Expression <Func <T, T2> > propertySelector,
        Expression <Func <T2, bool> > propertyPredicate)
    {
        var memberExpression = propertySelector.Body as MemberExpression;

        if (memberExpression == null)
        {
            throw new ArgumentException("propertySelector");
        }
        var expr     = Expression.Lambda <Func <T, bool> >(propertyPredicate.Body, propertySelector.Parameters);
        var rebinder = new ParameterToMemberExpressionRebinder(propertyPredicate.Parameters[0], memberExpression);

        expr = (Expression <Func <T, bool> >)rebinder.Visit(expr);
        return(expr);
    }
Beispiel #5
0
        public void ShouldChangeParameterForLambdaIntoMemberAccessor()
        {
            var data = new List <ExampleLeaf>
            {
                new ExampleLeaf
                {
                    SecuredRoot = new ExampleRoot
                    {
                        Id = 1
                    }
                },
                new ExampleLeaf
                {
                    SecuredRoot = new ExampleRoot
                    {
                        Id = 4
                    }
                },
                new ExampleLeaf
                {
                    SecuredRoot = new ExampleRoot
                    {
                        Id = 5
                    }
                }
            };

            Expression <Func <ExampleLeaf, ExampleRoot> > selector = x => x.SecuredRoot;
            List <long> ids = new List <long> {
                1, 2, 3
            };
            Expression <Func <ExampleRoot, bool> > predicate = x => ids.Contains(x.Id);

            // x => ids.Contains(x.SecuredRoot.Id)

            var resultingCombined = ParameterToMemberExpressionRebinder.CombineSinglePropertySelectorWithPredicate(selector, predicate);

            var resultingPredicate = resultingCombined.Compile();

            var results = data.Where(resultingPredicate);

            results.Should().HaveCount(1);
        }