Exemplo n.º 1
0
        public override Expression GetMemberExpression(Expression root, MappingEntity entity, MemberInfo member)
        {
            if (this.mapping.IsAssociationRelationship(entity, member))
            {
                var relatedEntity       = this.mapping.GetRelatedEntity(entity, member);
                var projection          = this.GetQueryExpression(relatedEntity);
                var declaredTypeMembers = this.mapping.GetAssociationKeyMembers(entity, member).ToList();
                var associatedMembers   = this.mapping.GetAssociationRelatedKeyMembers(entity, member).ToList();

                var where = null as Expression;

                for (int i = 0, n = associatedMembers.Count; i < n; i++)
                {
                    var equal = this.GetMemberExpression
                                (
                        projection.Projector,
                        relatedEntity,
                        associatedMembers[i]).Equal(this.GetMemberExpression(root, entity, declaredTypeMembers[i])
                                                    );

                    where = (where != null) ? where.And(equal) : equal;
                }

                var newAlias = new TableAlias();
                var pc       = DbColumnProjector.ProjectColumns
                               (
                    this.translator.Linguist.Language,
                    projection.Projector, null,
                    newAlias,
                    projection.Select.Alias
                               );

                var aggregator = DbAggregator.GetAggregator(TypeHelper.GetMemberType(member), typeof(IEnumerable <>).MakeGenericType(pc.Projector.Type));

                var result = new DbProjectionExpression
                             (
                    new DbSelectExpression(newAlias, pc.Columns, projection.Select, where),
                    pc.Projector, aggregator
                             );

                return(this.translator.Police.ApplyPolicy(result, member));
            }

            if (root is DbAliasedExpression aliasedRoot && this.mapping.IsColumn(entity, member))
            {
                return(new DbColumnExpression
                       (
                           TypeHelper.GetMemberType(member),
                           this.GetColumnType(entity, member),
                           aliasedRoot.Alias,
                           this.mapping.GetColumnName(entity, member)
                       ));
            }

            return(DbQueryBinder.BindMember(root, member));
        }
        protected Expression MakePredicate(Expression e1, Expression e2, IEnumerable <MemberInfo> members, bool negate)
        {
            var pred = members.Select(m => DbQueryBinder.BindMember(e1, m).Equal(DbQueryBinder.BindMember(e2, m))).Join(ExpressionType.And);

            if (negate)
            {
                pred = Expression.Not(pred);
            }

            return(pred);
        }
Exemplo n.º 3
0
        public virtual Expression Translate(Expression expression)
        {
            expression = DbQueryBinder.Bind(this, expression);
            expression = DbAggregateRewriter.Rewrite(this.Translator.Linguist.Language, expression);
            expression = DbUnusedColumnRemover.Remove(expression);
            expression = DbRedundantColumnRemover.Remove(expression);
            expression = DbRedundantSubqueryRemover.Remove(expression);
            expression = DbRedundantJoinRemover.Remove(expression);

            var bound = DbRelationshipBinder.Bind(this, expression);

            if (bound != expression)
            {
                expression = bound;
                expression = DbRedundantColumnRemover.Remove(expression);
                expression = DbRedundantJoinRemover.Remove(expression);
            }

            expression = DbComparisonRewriter.Rewrite(this.Mapping, expression);

            return(expression);
        }
Exemplo n.º 4
0
 public virtual Expression ApplyMapping(Expression expression)
 {
     return(DbQueryBinder.Bind(this, expression));
 }