protected virtual void OptimizeJoinClause( [NotNull] JoinClause joinClause, [NotNull] QueryModel queryModel, int index, [NotNull] Action baseVisitAction, [NotNull] MethodInfo operatorToFlatten, bool outerJoin = false) { Check.NotNull(joinClause, nameof(joinClause)); Check.NotNull(queryModel, nameof(queryModel)); Check.NotNull(baseVisitAction, nameof(baseVisitAction)); Check.NotNull(operatorToFlatten, nameof(operatorToFlatten)); RequiresClientJoin = true; var previousQuerySource = FindPreviousQuerySource(queryModel, index); var previousSelectExpression = previousQuerySource != null ? TryGetQuery(previousQuerySource) : null; var previousSelectProjectionCount = previousSelectExpression?.Projection.Count ?? -1; baseVisitAction(); if (previousSelectExpression != null) { var selectExpression = TryGetQuery(joinClause); if (selectExpression != null) { var sqlTranslatingExpressionVisitor = _sqlTranslatingExpressionVisitorFactory.Create(this); var predicate = sqlTranslatingExpressionVisitor .Visit( Expression.Equal( joinClause.OuterKeySelector, joinClause.InnerKeySelector)); if (predicate != null) { QueriesBySource.Remove(joinClause); previousSelectExpression.RemoveRangeFromProjection(previousSelectProjectionCount); var tableExpression = selectExpression.Tables.Single(); var projection = QueryCompilationContext .QuerySourceRequiresMaterialization(joinClause) ? selectExpression.Projection : Enumerable.Empty <Expression>(); var joinExpression = !outerJoin ? previousSelectExpression.AddInnerJoin(tableExpression, projection) : previousSelectExpression.AddOuterJoin(tableExpression, projection); joinExpression.Predicate = predicate; if (outerJoin) { var outerJoinOrderingExtractor = new OuterJoinOrderingExtractor(); outerJoinOrderingExtractor.Visit(predicate); foreach (var expression in outerJoinOrderingExtractor.Expressions) { previousSelectExpression .AddToOrderBy(new Ordering(expression, OrderingDirection.Asc)); } } Expression = _queryFlattenerFactory .Create( joinClause, QueryCompilationContext, operatorToFlatten, previousSelectProjectionCount) .Flatten((MethodCallExpression)Expression); RequiresClientJoin = false; } } } if (RequiresClientJoin) { CheckClientEval(joinClause); } }
public override void VisitAdditionalFromClause( AdditionalFromClause fromClause, QueryModel queryModel, int index) { Check.NotNull(fromClause, nameof(fromClause)); Check.NotNull(queryModel, nameof(queryModel)); base.VisitAdditionalFromClause(fromClause, queryModel, index); RequiresClientSelectMany = true; var selectExpression = TryGetQuery(fromClause); if (selectExpression != null && selectExpression.Tables.Count == 1) { var previousQuerySource = FindPreviousQuerySource(queryModel, index); if (previousQuerySource != null && !RequiresClientJoin) { var previousSelectExpression = TryGetQuery(previousQuerySource); if (previousSelectExpression != null) { if (!QueryCompilationContext.QuerySourceRequiresMaterialization(previousQuerySource)) { previousSelectExpression.ClearProjection(); previousSelectExpression.IsProjectStar = false; } var readerOffset = previousSelectExpression.Projection.Count; var correlated = selectExpression.IsCorrelated(); if (correlated) { if (!QueryCompilationContext.IsLateralJoinSupported) { return; } previousSelectExpression .AddLateralJoin(selectExpression.Tables.First(), selectExpression.Projection); } else { previousSelectExpression .AddCrossJoin(selectExpression.Tables.First(), selectExpression.Projection); } QueriesBySource.Remove(fromClause); Expression = _queryFlattenerFactory .Create( fromClause, QueryCompilationContext, LinqOperatorProvider.SelectMany, readerOffset) .Flatten((MethodCallExpression)Expression); RequiresClientSelectMany = false; } } } if (RequiresClientSelectMany) { CheckClientEval(fromClause); } }