protected override Expression VisitJoin(JoinExpression join) { if (join.Join == JoinType.SingletonLeftOuter) { // first visit right side w/o looking at condition Expression right = this.Visit(join.Right); AliasedExpression ax = right as AliasedExpression; if (ax != null && !this.allColumnsUsed.ContainsKey(ax.Alias)) { // if nothing references the alias on the right, then the join is redundant return(this.Visit(join.Left)); } // otherwise do it the right way Expression cond = this.Visit(join.Condition); Expression left = this.Visit(join.Left); right = this.Visit(join.Right); return(this.UpdateJoin(join, join.Join, left, right, cond)); } else { // visit join in reverse order Expression condition = this.Visit(join.Condition); Expression right = this.VisitSource(join.Right); Expression left = this.VisitSource(join.Left); return(this.UpdateJoin(join, join.Join, left, right, condition)); } }
protected virtual void AddAliases(Expression expr) { AliasedExpression ax = expr as AliasedExpression; if (ax != null) { this.AddAlias(ax.Alias); } else { JoinExpression jx = expr as JoinExpression; if (jx != null) { this.AddAliases(jx.Left); this.AddAliases(jx.Right); } } }
public override Expression GetMemberExpression(Expression root, MappingEntity entity, MemberInfo member) { if (this.mapping.IsAssociationRelationship(entity, member)) { MappingEntity relatedEntity = this.mapping.GetRelatedEntity(entity, member); ProjectionExpression projection = this.GetQueryExpression(relatedEntity); // make where clause for joining back to 'root' var declaredTypeMembers = this.mapping.GetAssociationKeyMembers(entity, member).ToList(); var associatedMembers = this.mapping.GetAssociationRelatedKeyMembers(entity, member).ToList(); Expression where = null; for (int i = 0, n = associatedMembers.Count; i < n; i++) { Expression equal = this.GetMemberExpression(projection.Projector, relatedEntity, associatedMembers[i]).Equal( this.GetMemberExpression(root, entity, declaredTypeMembers[i]) ); where = (where != null) ? where.And(equal) : equal; } TableAlias newAlias = new TableAlias(); var pc = ColumnProjector.ProjectColumns(this.translator.Linguist.Language, projection.Projector, null, newAlias, projection.Select.Alias); LambdaExpression aggregator = Aggregator.GetAggregator(TypeHelper.GetMemberType(member), typeof(IEnumerable <>).MakeGenericType(pc.Projector.Type)); var result = new ProjectionExpression( new SelectExpression(newAlias, pc.Columns, projection.Select, where), pc.Projector, aggregator ); return(this.translator.Police.ApplyPolicy(result, member)); } else { AliasedExpression aliasedRoot = root as AliasedExpression; if (aliasedRoot != null && this.mapping.IsColumn(entity, member)) { return(new ColumnExpression(TypeHelper.GetMemberType(member), this.GetColumnType(entity, member), aliasedRoot.Alias, this.mapping.GetColumnName(entity, member), new ColumnAlias(this.mapping.GetColumnAlias(entity, member)))); } return(QueryBinder.BindMember(root, member)); } }
protected override Expression VisitJoin(JoinExpression join) { Expression result = base.VisitJoin(join); join = result as JoinExpression; if (join != null) { AliasedExpression right = join.Right as AliasedExpression; if (right != null) { AliasedExpression similarRight = (AliasedExpression)this.FindSimilarRight(join.Left as JoinExpression, join); if (similarRight != null) { this.map.Add(right.Alias, similarRight.Alias); return(join.Left); } } } return(result); }