コード例 #1
0
        private LambdaExpression GetLambdaForEnumerableProperty <T>(IncludePathMetadataFragment includePathMetadataFragment, LambdaExpression filterLambda)
        {
            var lambdaType      = typeof(Func <,>).GetGenericTypeDefinition().MakeGenericType(includePathMetadataFragment.DeclaringType, includePathMetadataFragment.PropertyType);
            var memberParameter = (ParameterExpression)includePathMetadataFragment.PropertyAccessExpression.Expression;
            var propertyLambda  = Expression.Lambda(lambdaType, includePathMetadataFragment.PropertyAccessExpression, memberParameter);

            return(SecurityExtensions.CombineCollectionPropertySelectorWithPredicate(propertyLambda, filterLambda));
        }
コード例 #2
0
        public void ShouldReflectivelyUpdatePredicateForCollectionAccessors()
        {
            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 <ExampleRoot, bool> > predicate = x => ids.Contains(x.Id);
            LambdaExpression predicateExpression             = predicate;

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

            var resultingCombined = SecurityExtensions.CombineCollectionPropertySelectorWithPredicate(selectorExpression, predicateExpression);

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

            var results = data.Where(resultingPredicate);

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