Beispiel #1
0
        private MethodCallExpression CreateWhereCall <T>(int id, Type navigationProperyType) where T : class, IEntity
        {
            ParameterExpression pe = Expression.Parameter(navigationProperyType, "nav");

            var foreigKeys                = context.Model.GetEntityType(navigationProperyType).GetForeignKeys();
            var thePrincipalEntityKey     = foreigKeys.Single(fk => fk.PrincipalEntityType.ClrType == typeof(T));
            var thePrincipalEntityKeyName = thePrincipalEntityKey.Properties[0].Name;

            Expression left  = Expression.PropertyOrField(pe, thePrincipalEntityKeyName);
            Expression right = Expression.Constant(id, typeof(int?));
            Expression e1    = Expression.Equal(left, right);

            var constructorinfo = typeof(EntityQueryable <>).MakeGenericType(navigationProperyType).GetConstructors()[0];

            var  newstsy         = constructorinfo.Invoke(new[] { context.GetService <IEntityQueryProvider>() });
            Type func11          = typeof(EntityQueryable <>);
            Type generic23233    = func11.MakeGenericType(navigationProperyType);
            var  nwtwDynamically = Expression.Constant(newstsy, generic23233);

            Type func1111       = typeof(IQueryable <>);
            Type generic2323311 = func1111.MakeGenericType(navigationProperyType);

            Type func11111        = typeof(Func <,>);
            Type generic232331123 = func11111.MakeGenericType(navigationProperyType, typeof(bool));
            Type funxs            = typeof(Expression <>);
            Type skljslk          = funxs.MakeGenericType(generic232331123);

            var m1 = GetGenericMethod(typeof(Queryable), "Where", new[] { navigationProperyType }, generic2323311, skljslk);

            MethodCallExpression whereCallExpression = Expression.Call(
                m1,
                nwtwDynamically,        //nwtw
                Expression.Lambda(e1, new ParameterExpression[] { pe }));

            return(whereCallExpression);
        }
        private MethodCallExpression CreateWhereCall <T>(int id, Type navigationProperyType) where T : class, IEntity
        {
            ParameterExpression pe = Expression.Parameter(navigationProperyType, "nav");

            var foreigKeys                = _context.Model.GetEntityType(navigationProperyType).GetForeignKeys();
            var thePrincipalEntityKey     = foreigKeys.Single(fk => fk.PrincipalEntityType.ClrType == typeof(T));
            var thePrincipalEntityKeyName = thePrincipalEntityKey.Properties[0].Name; // TODO: Is it safe to asume first property?

            Expression left  = Expression.PropertyOrField(pe, thePrincipalEntityKeyName);
            Expression right = Expression.Constant(id, typeof(int?));
            Expression e1    = Expression.Equal(left, right);

            // TODO: Is it safe to asume first constructor?
            var constructorinfo = typeof(EntityQueryable <>).MakeGenericType(navigationProperyType).GetConstructors()[0];

            var  entityProviderConstructor = constructorinfo.Invoke(new[] { _context.GetService <IEntityQueryProvider>() });
            Type entityQuerableType        = typeof(EntityQueryable <>);
            Type entityQuerableOfNavigationPropertyType = entityQuerableType.MakeGenericType(navigationProperyType);
            var  nwtwDynamically = Expression.Constant(entityProviderConstructor, entityQuerableOfNavigationPropertyType);

            Type iQuerableType = typeof(IQueryable <>);
            Type iQuerableOfNavigationPropertyType = iQuerableType.MakeGenericType(navigationProperyType);

            Type func                = typeof(Func <,>);
            Type predicateFunc       = func.MakeGenericType(navigationProperyType, typeof(bool));
            Type expr                = typeof(Expression <>);
            Type expressionPredicate = expr.MakeGenericType(predicateFunc);

            var whereMethod = GetGenericMethod(typeof(Queryable), "Where",
                                               new[] { navigationProperyType },
                                               iQuerableOfNavigationPropertyType,
                                               expressionPredicate);

            MethodCallExpression whereCallExpression = Expression.Call(
                whereMethod,
                nwtwDynamically,        //nwtw
                Expression.Lambda(e1, new ParameterExpression[] { pe }));

            return(whereCallExpression);
        }