Ejemplo n.º 1
0
        public override Expression Apply(Expression expression, Random random)
        {
            var typeArgument = expression.Type.GetGenericArguments()[0];
            var navigations  = Context.Model.FindEntityType(typeArgument).GetNavigations().Where(n => n.IsCollection()).ToList();

            var i          = random.Next(navigations.Count);
            var navigation = navigations[i];

            var collectionElementType = navigation.ForeignKey.DeclaringEntityType.ClrType;
            var listType = typeof(List <>).MakeGenericType(collectionElementType);

            var select = SelectMethodInfo.MakeGenericMethod(typeArgument, listType);

            var where = EnumerableWhereMethodInfo.MakeGenericMethod(collectionElementType);
            var toList = ToListMethodInfo.MakeGenericMethod(collectionElementType);

            var outerPrm = Expression.Parameter(typeArgument, "outerPrm");
            var innerPrm = Expression.Parameter(collectionElementType, "innerPrm");

            var outerLambdaBody = Expression.Call(
                toList,
                Expression.Call(
                    where,
                    Expression.Property(outerPrm, navigation.PropertyInfo),
                    Expression.Lambda(Expression.Constant(true), innerPrm)));

            var resultExpression = Expression.Call(
                select,
                expression,
                Expression.Lambda(outerLambdaBody, outerPrm));

            return(resultExpression);
        }
Ejemplo n.º 2
0
        public override Expression Apply(Expression expression, Random random)
        {
            var typeArgument = expression.Type.GetGenericArguments()[0];
            var properties   = typeArgument.GetProperties().Where(p => !p.GetMethod.IsStatic).ToList();

            properties = FilterPropertyInfos(typeArgument, properties);

            var i = random.Next(properties.Count);

            var select = SelectMethodInfo.MakeGenericMethod(typeArgument, properties[i].PropertyType);
            var prm    = Expression.Parameter(typeArgument, "prm");

            var lambdaBody = (Expression)Expression.Property(prm, properties[i]);

            if (properties[i].PropertyType.IsValueType &&
                !(properties[i].PropertyType.IsGenericType &&
                  properties[i].PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)))
            {
                var nullablePropertyType = typeof(Nullable <>).MakeGenericType(properties[i].PropertyType);
                select     = SelectMethodInfo.MakeGenericMethod(typeArgument, nullablePropertyType);
                lambdaBody = Expression.Convert(lambdaBody, nullablePropertyType);
            }

            if (typeArgument == typeof(string))
            {
                // string.Length - make it nullable in case we access optional argument
                select     = SelectMethodInfo.MakeGenericMethod(typeArgument, typeof(int?));
                lambdaBody = Expression.Convert(lambdaBody, typeof(int?));
            }

            var lambda           = Expression.Lambda(lambdaBody, prm);
            var resultExpression = Expression.Call(select, expression, lambda);

            return(resultExpression);
        }
Ejemplo n.º 3
0
        protected Expression BuildSelectFirstOrDefaultPermissionEntityExpression(IEntityType entityType, JObject jObject, Expression queryableExpression, ParameterExpression parameterExpression, Expression selectBodyExpression)
        {
            var selectLambdaExpression       = Expression.Lambda(selectBodyExpression, parameterExpression);
            var selectCallExpression         = Expression.Call(SelectMethodInfo.MakeGenericMethod(entityType.ClrType, selectBodyExpression.Type), queryableExpression, selectLambdaExpression);
            var firstOrDefaultCallExpression = Expression.Call(null, FirstOrDefaultMethodInfo.MakeGenericMethod(selectBodyExpression.Type), selectCallExpression);

            return(firstOrDefaultCallExpression);
        }
Ejemplo n.º 4
0
        public override Expression Apply(Expression expression, Random random)
        {
            var typeArgument     = expression.Type.GetGenericArguments()[0];
            var select           = SelectMethodInfo.MakeGenericMethod(typeArgument, typeArgument);
            var prm              = Expression.Parameter(typeArgument, "prm");
            var lambda           = Expression.Lambda(prm, prm);
            var resultExpression = Expression.Call(select, expression, lambda);

            return(resultExpression);
        }