protected override Expression VisitClientJoin(ClientJoinExpression join) { // convert client join into a up-front lookup table builder and replace client-join in tree with lookup accessor // 1) lookup = query.Select(e => new KVP(key: inner, value: e)).ToLookup(kvp => kvp.Key, kvp => kvp.Value) Expression innerKey = MakeJoinKey(join.InnerKey); Expression outerKey = MakeJoinKey(join.OuterKey); ConstructorInfo kvpConstructor = typeof(KeyValuePair <,>).MakeGenericType(innerKey.Type, join.Projection.Projector.Type).GetConstructor( new[] { innerKey.Type, join.Projection.Projector.Type }); Expression constructKVPair = Expression.New(kvpConstructor, innerKey, join.Projection.Projector); ProjectionExpression newProjection = new ProjectionExpression(join.Projection.Source, constructKVPair); int iLookup = ++nLookup; Expression execution = ExecuteProjection(newProjection, false); ParameterExpression kvp = Expression.Parameter(constructKVPair.Type, "kvp"); // filter out nulls if (join.Projection.Projector.NodeType == (ExpressionType)DbExpressionType.OuterJoined) { LambdaExpression pred = Expression.Lambda( Expression.NotEqual( Expression.PropertyOrField(kvp, "Value"), Expression.Constant(null, join.Projection.Projector.Type) ), kvp ); execution = Expression.Call(typeof(Enumerable), "Where", new[] { kvp.Type }, execution, pred); } // make lookup LambdaExpression keySelector = Expression.Lambda(Expression.PropertyOrField(kvp, "Key"), kvp); LambdaExpression elementSelector = Expression.Lambda(Expression.PropertyOrField(kvp, "Value"), kvp); Expression toLookup = Expression.Call(typeof(Enumerable), "ToLookup", new[] { kvp.Type, outerKey.Type, join.Projection.Projector.Type }, execution, keySelector, elementSelector); // 2) agg(lookup[outer]) ParameterExpression lookup = Expression.Parameter(toLookup.Type, "lookup" + iLookup); PropertyInfo property = lookup.Type.GetProperty("Item"); Expression access = Expression.Call(lookup, property.GetGetMethod(), Visit(outerKey)); if (join.Projection.Aggregator != null) { // apply aggregator access = DbExpressionReplacer.Replace(join.Projection.Aggregator.Body, join.Projection.Aggregator.Parameters[0], access); } variables.Add(lookup); initializers.Add(toLookup); return(access); }
protected virtual Expression VisitClientJoin(ClientJoinExpression join) { ProjectionExpression projection = (ProjectionExpression)this.Visit(join.Projection); var outerKey = this.VisitExpressionList(join.OuterKey); var innerKey = this.VisitExpressionList(join.InnerKey); if (projection != join.Projection || outerKey != join.OuterKey || innerKey != join.InnerKey) { return(new ClientJoinExpression(projection, outerKey, innerKey)); } return(join); }
protected virtual Expression VisitClientJoin(ClientJoinExpression join) { this.AddAlias(join.Projection.Source.Alias); this.Write("ClientJoin("); this.WriteLine(Indentation.Inner); this.Write("OuterKey("); this.VisitExpressionList(join.OuterKey); this.Write("),"); this.WriteLine(Indentation.Same); this.Write("InnerKey("); this.VisitExpressionList(join.InnerKey); this.Write("),"); this.WriteLine(Indentation.Same); this.Visit(join.Projection); this.WriteLine(Indentation.Outer); this.Write(")"); return(join); }