Пример #1
0
        private static MemberAssignment GetCollectionNavigationExpression(Type sourcePropertyType, Type destinationPropertyType, Expression parameterExpression, PropertyInfo sourceProperty, MemberInfo destinationProperty, DbContext context)
        {
            // Prepare the inner lambda expression for the select statement
            sourcePropertyType      = sourcePropertyType.GenericTypeArguments[0];
            destinationPropertyType = destinationPropertyType.GenericTypeArguments[0];
            var childSourceProperties     = sourcePropertyType.GetProperties();
            var destinationProperties     = destinationPropertyType.GetProperties().Where(dest => dest.CanWrite);
            var selectParameterExpression = Expression.Parameter(sourcePropertyType);

            var bindings = GetBindings(destinationProperties, selectParameterExpression, destinationProperty, childSourceProperties, context);
            MemberInitExpression propertyInit = Expression.MemberInit(Expression.New(destinationPropertyType), bindings);
            Type function = typeof(Func <,>).MakeGenericType(sourcePropertyType, destinationPropertyType);

            // (t) => new T() { Property = t.Property, ... }
            LambdaExpression lambda     = Expression.Lambda(function, propertyInit, selectParameterExpression);
            MemberExpression collection = Expression.Property(parameterExpression, sourceProperty);

            IEnumerable <string> a = new List <string>();

            // p.Select(t => new T() { Property = t.Property, ... })
            Expression selectExpression = collection.CallSelect(lambda, destinationPropertyType);

            // Create select predicate
            return(Expression.Bind(destinationProperty, selectExpression));
        }