ProjectionPlanCompiler.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataWeb / Client / System / Data / Services / Client / ProjectionPlanCompiler.cs / 1305376 / ProjectionPlanCompiler.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//  
// Provides a class that can create a materialization plan for a
// projection. 
//  
//---------------------------------------------------------------------
 
//// Uncomment the following line to trace projection building activity.
////#define TRACE_CLIENT_PROJECTIONS

namespace System.Data.Services.Client 
{
    #region Namespaces. 
 
    using System;
    using System.Collections.Generic; 
    using System.Diagnostics;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection; 

    #endregion Namespaces. 
 
    /// 
    /// Use this class to create a  for a given projection lambda. 
    /// 
    internal class ProjectionPlanCompiler : ALinqExpressionVisitor
    {
        #region Private fields. 

        /// Annotations being tracked on this tree. 
        private readonly Dictionary annotations; 

        /// Expression that refers to the materializer. 
        private readonly ParameterExpression materializerExpression;

        /// Tracks rewrite-to-source rewrites introduced by expression normalizer.
        private readonly Dictionary normalizerRewrites; 

        /// Number to suffix to identifiers to help with debugging. 
        private int identifierId; 

        /// Path builder used to help with tracking state while compiling. 
        private ProjectionPathBuilder pathBuilder;

        /// Whether the top level projection has been found.
        private bool topLevelProjectionFound; 

        #endregion Private fields. 
 
        #region Constructors.
 
        /// 
        /// Initializes a new  instance.
        /// 
        /// Rewrites introduces by normalizer. 
        private ProjectionPlanCompiler(Dictionary normalizerRewrites)
        { 
            this.annotations = new Dictionary(ReferenceEqualityComparer.Instance); 
            this.materializerExpression = Expression.Parameter(typeof(object), "mat");
            this.normalizerRewrites = normalizerRewrites; 
            this.pathBuilder = new ProjectionPathBuilder();
        }

        #endregion Constructors. 

        #region Internal methods. 
 
        /// Creates a projection plan from the specified .
        /// Projection expression. 
        /// Tracks rewrite-to-source rewrites introduced by expression normalizer.
        /// A new  instance.
        internal static ProjectionPlan CompilePlan(LambdaExpression projection, Dictionary normalizerRewrites)
        { 
            Debug.Assert(projection != null, "projection != null");
            Debug.Assert(projection.Parameters.Count == 1, "projection.Parameters.Count == 1"); 
            Debug.Assert( 
                projection.Body.NodeType == ExpressionType.Constant ||
                projection.Body.NodeType == ExpressionType.MemberInit || 
                projection.Body.NodeType == ExpressionType.MemberAccess ||
                projection.Body.NodeType == ExpressionType.Convert ||
                projection.Body.NodeType == ExpressionType.ConvertChecked ||
                projection.Body.NodeType == ExpressionType.New, 
                "projection.Body.NodeType == Constant, MemberInit, MemberAccess, Convert(Checked) New");
 
            ProjectionPlanCompiler rewriter = new ProjectionPlanCompiler(normalizerRewrites); 
#if TRACE_CLIENT_PROJECTIONS
            Trace.WriteLine("Projection: " + projection); 
#endif

            Expression plan = rewriter.Visit(projection);
#if TRACE_CLIENT_PROJECTIONS 
            Trace.WriteLine("Becomes: " + plan);
#endif 
 
            ProjectionPlan result = new ProjectionPlan();
            result.Plan = (Func)((LambdaExpression)plan).Compile(); 
            result.ProjectedType = projection.Body.Type;
#if DEBUG
            result.SourceProjection = projection;
            result.TargetProjection = plan; 
#endif
            return result; 
        } 

        /// Binary visit method. 
        /// Binary expression to visit.
        /// (Possibly rewritten) binary expression.
        /// 
        /// This override is introduced because binary expressions are one of 
        /// the scopes at which normalization happens.
        ///  
        internal override Expression VisitBinary(BinaryExpression b) 
        {
            Expression original = this.GetExpressionBeforeNormalization(b); 
            if (original == b)
            {
                return base.VisitBinary(b);
            } 
            else
            { 
                return this.Visit(original); 
            }
        } 

        /// Visits the specified  expression.
        /// Expression to check.
        /// The visited expression. 
        /// 
        /// This override allows us to check for rewrites created by 
        /// ExpressionNormalizer.CreateCompareExpression. 
        /// 
        internal override Expression VisitConditional(ConditionalExpression conditional) 
        {
            Debug.Assert(conditional != null, "conditional != null");
            Expression original = this.GetExpressionBeforeNormalization(conditional);
            if (original != conditional) 
            {
                return this.Visit(original); 
            } 

            var nullCheck = ResourceBinder.PatternRules.MatchNullCheck(this.pathBuilder.LambdaParameterInScope, conditional); 
            if (!nullCheck.Match || !ClientType.CheckElementTypeIsEntity(nullCheck.AssignExpression.Type))
            {
                return base.VisitConditional(conditional);
            } 

            return this.RebindConditionalNullCheck(conditional, nullCheck); 
        } 

        /// Unary visit method. 
        /// Unary expression to visit.
        /// (Possibly rewritten) unary expression.
        /// 
        /// This override is introduced because unary expressions are one of 
        /// the scopes at which normalization happens.
        ///  
        internal override Expression VisitUnary(UnaryExpression u) 
        {
            Expression original = this.GetExpressionBeforeNormalization(u); 
            Expression result;
            if (original == u)
            {
                result = base.VisitUnary(u); 
                UnaryExpression unaryResult = result as UnaryExpression;
                if (unaryResult != null) 
                { 
                    ExpressionAnnotation annotation;
                    if (this.annotations.TryGetValue(unaryResult.Operand, out annotation)) 
                    {
                        this.annotations[result] = annotation;
                    }
                } 
            }
            else 
            { 
                result = this.Visit(original);
            } 

            return result;
        }
 
        /// 
        /// MemberExpression visit method 
        ///  
        /// The MemberExpression expression to visit
        /// The visited MemberExpression expression  
        internal override Expression VisitMemberAccess(MemberExpression m)
        {
            Debug.Assert(m != null, "m != null");
 
            Expression result;
            Expression baseSourceExpression = m.Expression; 
 
            // if primitive or nullable primitive, allow member access... i.e. calling Value on nullable
            if (ClientConvert.IsKnownNullableType(baseSourceExpression.Type)) 
            {
                result = base.VisitMemberAccess(m);
            }
            else 
            {
                Expression baseTargetExpression = this.Visit(baseSourceExpression); 
                ExpressionAnnotation annotation; 
                if (this.annotations.TryGetValue(baseTargetExpression, out annotation))
                { 
                    result = this.RebindMemberAccess(m, annotation);
                }
                else
                { 
                    result = Expression.MakeMemberAccess(baseTargetExpression, m.Member);
                } 
            } 

            return result; 
        }

        /// Parameter visit method.
        /// Parameter to visit. 
        /// Resulting expression.
        ///  
        /// The parameter may get rewritten as a materializing projection if 
        /// it refers to an entity outside of member binding. In this case,
        /// it becomes a standalone tracked entity. 
        /// 
        internal override Expression VisitParameter(ParameterExpression p)
        {
            Debug.Assert(p != null, "p != null"); 

            // If this parameter isn't interesting, we're not doing any rewrites. 
            Expression result; 
            ExpressionAnnotation annotation;
            if (this.annotations.TryGetValue(p, out annotation)) 
            {
                result = this.RebindParameter(p, annotation);
            }
            else 
            {
                result = base.VisitParameter(p); 
            } 

            return result; 
        }

        /// 
        /// MemberInitExpression visit method 
        /// 
        /// The MemberInitExpression to visit 
        /// The visited MemberInitExpression 
        /// 
        /// A MemberInitExpression on a knownEntityType implies that we 
        /// want to materialize the thing.
        /// 
        internal override Expression VisitMemberInit(MemberInitExpression init)
        { 
            this.pathBuilder.EnterMemberInit(init);
 
            Expression result = null; 
            if (this.pathBuilder.CurrentIsEntity && init.Bindings.Count > 0)
            { 
                result = this.RebindEntityMemberInit(init);
            }
            else
            { 
                result = base.VisitMemberInit(init);
            } 
 
            this.pathBuilder.LeaveMemberInit();
            return result; 
        }

        /// Visits a method call expression.
        /// Expression to visit. 
        /// A (possibly rewritten) expression for .
        internal override Expression VisitMethodCall(MethodCallExpression m) 
        { 
            Debug.Assert(m != null, "m != null");
 
            Expression original = this.GetExpressionBeforeNormalization(m);
            if (original != m)
            {
                return this.Visit(original); 
            }
 
            Expression result; 
            if (this.pathBuilder.CurrentIsEntity)
            { 
                Debug.Assert(
                    ProjectionAnalyzer.IsMethodCallAllowedEntitySequence(m) || ResourceBinder.PatternRules.MatchReferenceEquals(m),
                    "ProjectionAnalyzer.IsMethodCallAllowedEntitySequence(m) || ResourceBinder.PatternRules.MatchReferenceEquals(m) -- otherwise ProjectionAnalyzer should have blocked this for entities");
                if (m.Method.Name == "Select") 
                {
                    result = this.RebindMethodCallForMemberSelect(m); 
                } 
                else if (m.Method.Name == "ToList")
                { 
                    result = this.RebindMethodCallForMemberToList(m);
                }
                else
                { 
                    Debug.Assert(m.Method.Name == "ReferenceEquals", "We don't know how to handle this method, ProjectionAnalyzer updated?");
                    result = base.VisitMethodCall(m); 
                } 
            }
            else 
            {
                if (ProjectionAnalyzer.IsMethodCallAllowedEntitySequence(m))
                {
                    result = this.RebindMethodCallForNewSequence(m); 
                }
                else 
                { 
                    result = base.VisitMethodCall(m);
                } 
            }

            return result;
        } 

        /// Visits a new expression 
        /// Expression to visit 
        /// A (possibly rewritten) expression for .
        internal override NewExpression VisitNew(NewExpression nex) 
        {
            Debug.Assert(nex != null, "nex != null");

            // Special case DataServiceCollection creation so context instance 
            // and paging (continuations) propperly flow through
            if (ResourceBinder.PatternRules.MatchNewDataServiceCollectionOfT(nex)) 
            { 
                return this.RebindNewExpressionForDataServiceCollectionOfT(nex);
            } 

            return base.VisitNew(nex);
        }
 
        /// LambdaExpression visit method.
        /// The LambdaExpression to visit 
        /// The visited LambdaExpression 
        internal override Expression VisitLambda(LambdaExpression lambda)
        { 
            Debug.Assert(lambda != null, "lambda != null");

            Expression result;
            if (!this.topLevelProjectionFound || lambda.Parameters.Count == 1 && ClientType.CheckElementTypeIsEntity(lambda.Parameters[0].Type)) 
            {
                this.topLevelProjectionFound = true; 
 
                ParameterExpression expectedTypeParameter = Expression.Parameter(typeof(Type), "type" + this.identifierId);
                ParameterExpression entryParameter = Expression.Parameter(typeof(object), "entry" + this.identifierId); 
                this.identifierId++;

                this.pathBuilder.EnterLambdaScope(lambda, entryParameter, expectedTypeParameter);
                ProjectionPath parameterPath = new ProjectionPath(lambda.Parameters[0], expectedTypeParameter, entryParameter); 
                ProjectionPathSegment parameterSegment = new ProjectionPathSegment(parameterPath, null, null);
                parameterPath.Add(parameterSegment); 
                this.annotations[lambda.Parameters[0]] = new ExpressionAnnotation() { Segment = parameterSegment }; 

                Expression body = this.Visit(lambda.Body); 

                // Value types must be boxed explicitly; the lambda initialization
                // won't do it for us (type-compatible types still work, so all
                // references will work fine with System.Object). 
                if (body.Type.IsValueType)
                { 
                    body = Expression.Convert(body, typeof(object)); 
                }
 
                result = Expression.Lambda>(
                    body,
                    this.materializerExpression,
                    entryParameter, 
                    expectedTypeParameter);
 
                this.pathBuilder.LeaveLambdaScope(); 
            }
            else 
            {
                result = base.VisitLambda(lambda);
            }
 
            return result;
        } 
 
        #endregion Internal methods.
 
        #region Private methods.

        /// Generates a call to a static method on AtomMaterializer.
        /// Name of method to invoke. 
        /// Arguments to pass to method.
        /// The constructed expression. 
        ///  
        /// There is no support for overload resolution - method names in AtomMaterializer
        /// must be unique. 
        /// 
        private static Expression CallMaterializer(string methodName, params Expression[] arguments)
        {
            return CallMaterializerWithType(methodName, null, arguments); 
        }
 
        /// Generates a call to a static method on AtomMaterializer. 
        /// Name of method to invoke.
        /// Type arguments for method (possibly null). 
        /// Arguments to pass to method.
        /// The constructed expression.
        /// 
        /// There is no support for overload resolution - method names in AtomMaterializer 
        /// must be unique.
        ///  
        private static Expression CallMaterializerWithType(string methodName, Type[] typeArguments, params Expression[] arguments) 
        {
            Debug.Assert(methodName != null, "methodName != null"); 
            Debug.Assert(arguments != null, "arguments != null");

            MethodInfo method = typeof(AtomMaterializerInvoker).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
            Debug.Assert(method != null, "method != null - found " + methodName); 
            if (typeArguments != null)
            { 
                method = method.MakeGenericMethod(typeArguments); 
            }
 
            return Expression.Call(method, arguments);
        }

        /// Creates an expression that calls ProjectionCheckValueForPathIsNull. 
        /// Expression for root entry for paths.
        /// Expression for expected type for entry. 
        /// Path to check null value for. 
        /// A new expression with the call instance.
        private Expression CallCheckValueForPathIsNull(Expression entry, Expression entryType, ProjectionPath path) 
        {
            Expression result = CallMaterializer("ProjectionCheckValueForPathIsNull", entry, entryType, Expression.Constant(path, typeof(object)));
            this.annotations.Add(result, new ExpressionAnnotation() { Segment = path[path.Count - 1] });
            return result; 
        }
 
        /// Creates an expression that calls ProjectionValueForPath. 
        /// Expression for root entry for paths.
        /// Expression for expected type for entry. 
        /// Path to pull value from.
        /// A new expression with the call instance.
        private Expression CallValueForPath(Expression entry, Expression entryType, ProjectionPath path)
        { 
            Debug.Assert(entry != null, "entry != null");
            Debug.Assert(path != null, "path != null"); 
 
            Expression result = CallMaterializer("ProjectionValueForPath", this.materializerExpression, entry, entryType, Expression.Constant(path, typeof(object)));
            this.annotations.Add(result, new ExpressionAnnotation() { Segment = path[path.Count - 1] }); 
            return result;
        }

        /// Creates an expression that calls ProjectionValueForPath. 
        /// Expression for root entry for paths.
        /// Expression for expected type for entry. 
        /// Path to pull value from. 
        /// Path to convert result for.
        /// A new expression with the call instance. 
        private Expression CallValueForPathWithType(Expression entry, Expression entryType, ProjectionPath path, Type type)
        {
            Debug.Assert(entry != null, "entry != null");
            Debug.Assert(path != null, "path != null"); 

            Expression value = this.CallValueForPath(entry, entryType, path); 
            Expression result = Expression.Convert(value, type); 
            this.annotations.Add(result, new ExpressionAnnotation() { Segment = path[path.Count - 1] });
            return result; 
        }

        /// 
        /// Rebinds a conditional that performs a null check on an entity. 
        /// 
        /// Conditional expression. 
        /// Results of null check analysis. 
        /// The rebound expression.
        ///  
        /// Do a rewrite to avoid creating a type in the null check:
        ///   a.b == null ? null : [a.b]-based expression
        /// becomes
        ///   ProjectionIsNull(a.b) ? null : [a.b]-based expression 
        /// 
        private Expression RebindConditionalNullCheck(ConditionalExpression conditional, ResourceBinder.PatternRules.MatchNullCheckResult nullCheck) 
        { 
            Debug.Assert(conditional != null, "conditional != null");
            Debug.Assert(nullCheck.Match, "nullCheck.Match -- otherwise no reason to call this rebind method"); 

            Expression testToNullForProjection = this.Visit(nullCheck.TestToNullExpression);
            Expression assignForProjection = this.Visit(nullCheck.AssignExpression);
            ExpressionAnnotation testToNullAnnotation; 
            if (!this.annotations.TryGetValue(testToNullForProjection, out testToNullAnnotation))
            { 
                return base.VisitConditional(conditional); 
            }
 
            ProjectionPathSegment testToNullSegment = testToNullAnnotation.Segment;

            Expression testToNullThroughMethod = this.CallCheckValueForPathIsNull(
                testToNullSegment.StartPath.RootEntry, 
                testToNullSegment.StartPath.ExpectedRootType,
                testToNullSegment.StartPath); 
 
            Expression test = testToNullThroughMethod;
            Expression iftrue = Expression.Constant(null, assignForProjection.Type); 
            Expression iffalse = assignForProjection;
            Expression result = Expression.Condition(test, iftrue, iffalse);
            return result;
        } 

        ///  
        /// Rebinds the specified  expression by gathering 
        /// annotated paths and returning an expression that calls the
        /// ProjectionGetEntity method. 
        /// 
        /// Member initialization expression.
        /// A new expression suitable for materialization.
        private Expression RebindEntityMemberInit(MemberInitExpression init) 
        {
            Debug.Assert(init != null, "init != null"); 
            Debug.Assert(init.Bindings.Count > 0, "init.Bindings.Count > 0 -- otherwise this is just empty construction"); 

            // We "jump" into entities only if we're not already materializing an entity. 
            Expression[] expressions;
            if (!this.pathBuilder.HasRewrites)
            {
                MemberAssignmentAnalysis propertyAnalysis = MemberAssignmentAnalysis.Analyze( 
                    this.pathBuilder.LambdaParameterInScope,
                    ((MemberAssignment)init.Bindings[0]).Expression); 
                expressions = propertyAnalysis.GetExpressionsToTargetEntity(); 
                Debug.Assert(expressions.Length != 0, "expressions.Length != 0 -- otherwise there is no correlation to parameter in entity member init");
            } 
            else
            {
                expressions = MemberAssignmentAnalysis.EmptyExpressionArray;
            } 

            Expression entryParameterAtMemberInit = this.pathBuilder.ParameterEntryInScope; 
            List propertyNames = new List(); 
            List> propertyFunctions = new List>();
            Type projectedType = init.NewExpression.Type; 
            Expression projectedTypeExpression = Expression.Constant(projectedType, typeof(Type));

            // We may need to materialize from deeper in the entity tree for anonymous types.
            // t => new { nested = new Nested() { nid = t.nested.nid } 
            //
            // We do the same kind of rewriting we'd do for a nested entity 
            // but at the initializing scope (rather than at the member assignment scope). 
            //
            // t=> new { nested = ProjInit(GetEntry(entry0, "Nested"), "nid", *->nid) } 
            Expression entryToInitValue;    // Expression that yields value for entry in target tree.
            Expression expectedParamValue;  // Expression that yield expectedType in target tree.
            ParameterExpression entryParameterForMembers;       // Parameter expression members think of as "entry".
            ParameterExpression expectedParameterForMembers;    // Parameter expression members think of as "expectedType" for entry. 
            string[] expressionNames = expressions.Skip(1).Select(e => ((MemberExpression)e).Member.Name).ToArray();
            if (expressions.Length <= 1) 
            { 
                entryToInitValue = this.pathBuilder.ParameterEntryInScope;
                expectedParamValue = this.pathBuilder.ExpectedParamTypeInScope; 
                entryParameterForMembers = (ParameterExpression)this.pathBuilder.ParameterEntryInScope;
                expectedParameterForMembers = (ParameterExpression)this.pathBuilder.ExpectedParamTypeInScope;
            }
            else 
            {
                entryToInitValue = this.GetDeepestEntry(expressions); 
                expectedParamValue = projectedTypeExpression; 
                entryParameterForMembers = Expression.Parameter(typeof(object), "subentry" + this.identifierId++);
                expectedParameterForMembers = (ParameterExpression)this.pathBuilder.ExpectedParamTypeInScope; 

                // Annotate the entry expression with 'how we get to it' information.
                // The annotation on entryToInitiValue is picked up
                // The annotation on entryParameterForMembers is picked up to build nested member-init on entities. 
                ProjectionPath entryPath = new ProjectionPath(
                    (ParameterExpression)this.pathBuilder.LambdaParameterInScope, 
                    this.pathBuilder.ExpectedParamTypeInScope, 
                    this.pathBuilder.ParameterEntryInScope,
                    expressions.Skip(1)); 

                this.annotations.Add(entryToInitValue, new ExpressionAnnotation() { Segment = entryPath[entryPath.Count - 1] });
                this.annotations.Add(entryParameterForMembers, new ExpressionAnnotation() { Segment = entryPath[entryPath.Count - 1] });
                this.pathBuilder.RegisterRewrite(this.pathBuilder.LambdaParameterInScope, expressionNames, entryParameterForMembers); 
            }
 
            for (int i = 0; i < init.Bindings.Count; i++) 
            {
                MemberAssignment assignment = (MemberAssignment)init.Bindings[i]; 
                propertyNames.Add(assignment.Member.Name);

                LambdaExpression propertyLambda;
 
                // Here are the rewrites we do for member inits:
                // new T { id = t.id } 
                // => ProjInit(pt, "id", f(t -> *.id)); 
                //
                // new T { t2 = new T2 { id2 = *.t2.id2 } } 
                // => ProjInit(pt, "t2", f(ProjInit(pt->t2), "id2", *.id2)))
                if ((ClientType.CheckElementTypeIsEntity(assignment.Member.ReflectedType) &&
                     assignment.Expression.NodeType == ExpressionType.MemberInit))
                { 
                    Expression nestedEntry = CallMaterializer(
                        "ProjectionGetEntry", 
                        entryParameterAtMemberInit, 
                        Expression.Constant(assignment.Member.Name, typeof(string)));
                    ParameterExpression nestedEntryParameter = Expression.Parameter( 
                        typeof(object),
                        "subentry" + this.identifierId++);

                    // Register the rewrite from the top to the entry if necessary. 
                    ProjectionPath entryPath;
                    ExpressionAnnotation entryAnnotation; 
                    if (this.annotations.TryGetValue(this.pathBuilder.ParameterEntryInScope, out entryAnnotation)) 
                    {
                        entryPath = new ProjectionPath( 
                            (ParameterExpression)this.pathBuilder.LambdaParameterInScope,
                            this.pathBuilder.ExpectedParamTypeInScope,
                            entryParameterAtMemberInit);
                        entryPath.AddRange(entryAnnotation.Segment.StartPath); 
                    }
                    else 
                    { 
                        entryPath = new ProjectionPath(
                            (ParameterExpression)this.pathBuilder.LambdaParameterInScope, 
                            this.pathBuilder.ExpectedParamTypeInScope,
                            entryParameterAtMemberInit,
                            expressions.Skip(1));
                    } 

                    ProjectionPathSegment nestedSegment = new ProjectionPathSegment( 
                        entryPath, 
                        assignment.Member.Name,
                        assignment.Member.ReflectedType); 

                    entryPath.Add(nestedSegment);

                    string[] names = (entryPath.Where(m => m.Member != null).Select(m => m.Member)).ToArray(); 

                    this.annotations.Add(nestedEntryParameter, new ExpressionAnnotation() { Segment = nestedSegment }); 
                    this.pathBuilder.RegisterRewrite(this.pathBuilder.LambdaParameterInScope, names, nestedEntryParameter); 
                    Expression e = this.Visit(assignment.Expression);
                    this.pathBuilder.RevokeRewrite(this.pathBuilder.LambdaParameterInScope, names); 
                    this.annotations.Remove(nestedEntryParameter);

                    e = Expression.Convert(e, typeof(object));
                    ParameterExpression[] parameters = 
                        new ParameterExpression[]
                        { 
                            this.materializerExpression, 
                            nestedEntryParameter,
                            expectedParameterForMembers, 
                        };
                    propertyLambda = Expression.Lambda(e, parameters);

                    Expression[] nestedParams = 
                        new Expression[]
                        { 
                            this.materializerExpression, 
                            nestedEntry,
                            expectedParameterForMembers, 
                        };
                    var invokeParameters =
                        new ParameterExpression[]
                        { 
                            this.materializerExpression,
                            (ParameterExpression)entryParameterAtMemberInit, 
                            expectedParameterForMembers, 
                        };
                    propertyLambda = Expression.Lambda(Expression.Invoke(propertyLambda, nestedParams), invokeParameters); 
                }
                else
                {
                    // We need an expression of object, which might require boxing. 
                    Expression e = this.Visit(assignment.Expression);
                    e = Expression.Convert(e, typeof(object)); 
                    ParameterExpression[] parameters = 
                        new ParameterExpression[]
                        { 
                            this.materializerExpression,
                            entryParameterForMembers,
                            expectedParameterForMembers,
                        }; 
                    propertyLambda = Expression.Lambda(e, parameters);
                } 
 
#if TRACE_CLIENT_PROJECTIONS
                Trace.WriteLine("Compiling lambda for " + assignment.Member.Name + ": " + propertyLambda); 
#endif
                propertyFunctions.Add((Func) propertyLambda.Compile());
            }
 
            // Revoke rewrites used for nested initialization.
            for (int i = 1; i < expressions.Length; i++) 
            { 
                this.pathBuilder.RevokeRewrite(this.pathBuilder.LambdaParameterInScope, expressionNames);
                this.annotations.Remove(entryToInitValue); 
                this.annotations.Remove(entryParameterForMembers);
            }

            Expression reboundExpression = CallMaterializer( 
                "ProjectionInitializeEntity",
                this.materializerExpression, 
                entryToInitValue, 
                expectedParamValue,
                projectedTypeExpression, 
                Expression.Constant(propertyNames.ToArray()),
                Expression.Constant(propertyFunctions.ToArray()));

            return Expression.Convert(reboundExpression, projectedType); 
        }
 
        ///  
        /// Creates an expression that gets the deepest entry that will be found on the
        /// specified  (for the target tree). 
        /// 
        /// Path of expressions to walk.
        /// An expression that invokes ProjectionGetEntry on the target tree.
        private Expression GetDeepestEntry(Expression[] path) 
        {
            Debug.Assert(path.Length > 1, "path.Length > 1"); 
 
            Expression result = null;
            int pathIndex = 1; 
            do
            {
                result = CallMaterializer(
                    "ProjectionGetEntry", 
                    result ?? this.pathBuilder.ParameterEntryInScope,
                    Expression.Constant(((MemberExpression)path[pathIndex]).Member.Name, typeof(string))); 
                pathIndex++; 
            }
            while (pathIndex < path.Length); 

            return result;
        }
 
        /// Gets an expression before its rewrite.
        /// Expression to check. 
        /// The expression before normalization. 
        private Expression GetExpressionBeforeNormalization(Expression expression)
        { 
            Debug.Assert(expression != null, "expression != null");
            if (this.normalizerRewrites != null)
            {
                Expression original; 
                if (this.normalizerRewrites.TryGetValue(expression, out original))
                { 
                    expression = original; 
                }
            } 

            return expression;
        }
 
        /// Rebinds the specified parameter expression as a path-based access.
        /// Expression to rebind. 
        /// Annotation for the expression to rebind. 
        /// The rebound expression.
        private Expression RebindParameter(Expression expression, ExpressionAnnotation annotation) 
        {
            Debug.Assert(expression != null, "expression != null");
            Debug.Assert(annotation != null, "annotation != null");
 
            Expression result;
            result = this.CallValueForPathWithType( 
                annotation.Segment.StartPath.RootEntry, 
                annotation.Segment.StartPath.ExpectedRootType,
                annotation.Segment.StartPath, 
                expression.Type);

            // Refresh the annotation so the next one that comes along
            // doesn't start off with an already-written path. 
            ProjectionPath parameterPath = new ProjectionPath(
                annotation.Segment.StartPath.Root, 
                annotation.Segment.StartPath.ExpectedRootType, 
                annotation.Segment.StartPath.RootEntry);
            ProjectionPathSegment parameterSegment = new ProjectionPathSegment(parameterPath, null, null); 
            parameterPath.Add(parameterSegment);
            this.annotations[expression] = new ExpressionAnnotation() { Segment = parameterSegment };

            return result; 
        }
 
        /// Rebinds the specified member access expression into a path-based value retrieval method call. 
        /// Member expression.
        /// Annotation for the base portion of the expression. 
        /// A rebound expression.
        private Expression RebindMemberAccess(MemberExpression m, ExpressionAnnotation baseAnnotation)
        {
            Debug.Assert(m != null, "m != null"); 
            Debug.Assert(baseAnnotation != null, "baseAnnotation != null");
 
            ProjectionPathSegment memberSegment; 

            // If we are in nested member-init, we rewrite the property 
            // accessors that are in the form of top.nested.id to
            // nested.id.
            Expression baseSourceExpression = m.Expression;
            Expression result = this.pathBuilder.GetRewrite(baseSourceExpression); 
            if (result != null)
            { 
                Expression baseTypeExpression = Expression.Constant(baseSourceExpression.Type, typeof(Type)); 
                ProjectionPath nestedPath = new ProjectionPath(result as ParameterExpression, baseTypeExpression, result);
                ProjectionPathSegment nestedSegment = new ProjectionPathSegment(nestedPath, m.Member.Name, m.Type); 
                nestedPath.Add(nestedSegment);
                result = this.CallValueForPathWithType(result, baseTypeExpression, nestedPath, m.Type);
            }
            else 
            {
                // This actually modifies the path for the underlying 
                // segments, but that shouldn't be a problem. Actually 
                // we should be able to remove it from the dictionary.
                // There should be no aliasing problems, because 
                // annotations always come from target expression
                // that are generated anew (except parameters,
                // but those)
                memberSegment = new ProjectionPathSegment(baseAnnotation.Segment.StartPath, m.Member.Name, m.Type); 
                baseAnnotation.Segment.StartPath.Add(memberSegment);
                result = this.CallValueForPathWithType( 
                    baseAnnotation.Segment.StartPath.RootEntry, 
                    baseAnnotation.Segment.StartPath.ExpectedRootType,
                    baseAnnotation.Segment.StartPath, 
                    m.Type);
            }

            return result; 
        }
 
        /// Rewrites a new statement for DataServiceCollection so the paging information 
        /// is preserved in the materializer.
        /// NewExpression to create a collection 
        /// The rewritten expression.
        private NewExpression RebindNewExpressionForDataServiceCollectionOfT(NewExpression nex)
        {
            Debug.Assert(nex != null, "nex != null"); 
            Debug.Assert(
                ResourceBinder.PatternRules.MatchNewDataServiceCollectionOfT(nex), 
                "Called should have checked that the 'new' was for our collection type"); 

            NewExpression result = base.VisitNew(nex); 

            ExpressionAnnotation annotation = null;

            if (result != null) 
            {
                ConstructorInfo constructorInfo = 
                    nex.Type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First( 
                        c => c.GetParameters().Length == 7 && c.GetParameters()[0].ParameterType == typeof(object));
 
                Type enumerable = typeof(IEnumerable<>).MakeGenericType(nex.Type.GetGenericArguments()[0]);

                if (result.Arguments.Count == 1 && result.Constructor == nex.Type.GetConstructor(new[] { enumerable }) &&
                    this.annotations.TryGetValue(result.Arguments[0], out annotation)) 
                {
                    // DataServiceCollection( 
                    //     IEnumerable items) 
                    // ->
                    // DataServiceCollection(materializer, null, items, TrackingMode.AutoChangeTracking, null, null, null) 
                    result = Expression.New(
                        constructorInfo,
                        this.materializerExpression,
                        Expression.Constant(null, typeof(DataServiceContext)), 
                        result.Arguments[0],
                        Expression.Constant(TrackingMode.AutoChangeTracking, typeof(TrackingMode)), 
                        Expression.Constant(null, typeof(string)), 
                        Expression.Constant(null, typeof(Func)),
                        Expression.Constant(null, typeof(Func))); 
                }
                else if (result.Arguments.Count == 2 &&
                         this.annotations.TryGetValue(result.Arguments[0], out annotation))
                { 
                    // DataServiceCollection(
                    //     IEnumerable items, 
                    //     TrackingMode trackingMode) 
                    // ->
                    // DataServiceCollection(materializer, null, items, trackingMode, null, null, null) 
                    result = Expression.New(
                        constructorInfo,
                        this.materializerExpression,
                        Expression.Constant(null, typeof(DataServiceContext)), 
                        result.Arguments[0], // items
                        result.Arguments[1], // TrackingMode 
                        Expression.Constant(null, typeof(string)), 
                        Expression.Constant(null, typeof(Func)),
                        Expression.Constant(null, typeof(Func))); 
                }
                else if (result.Arguments.Count == 5 &&
                         this.annotations.TryGetValue(result.Arguments[0], out annotation))
                { 
                    // DataServiceCollection(
                    //     IEnumerable items, 
                    //     TrackingMode trackingMode, 
                    //     string entitySet,
                    //     Func<> entityChangedCallback, 
                    //     Func<> entityCollectionChangedCallback)
                    // ->
                    // DataServiceCollection(materializer, null, items, trackingMode,
                    //     entitySet, entityChangedCallback, entityCollectionChangedCallback) 
                    result = Expression.New(
                        constructorInfo, 
                        this.materializerExpression, 
                        Expression.Constant(null, typeof(DataServiceContext)),
                        result.Arguments[0],   // items 
                        result.Arguments[1],   // TrackingMode
                        result.Arguments[2],   // entityset name
                        result.Arguments[3],   // entity changed cb
                        result.Arguments[4]);  // collection changed cb 
                }
                else if (result.Arguments.Count == 6 && 
                         typeof(DataServiceContext).IsAssignableFrom(result.Arguments[0].Type) && 
                         this.annotations.TryGetValue(result.Arguments[1], out annotation))
                { 
                    // DataServiceCollection(
                    //     DataServiceContext context,
                    //     IEnumerable items,
                    //     TrackingMode trackingMode, 
                    //     string entitySet,
                    //     Func<> entityChangedCallback, 
                    //     Func<> entityCollectionChangedCallback) 
                    // ->
                    // DataServiceCollection(materializer, context, items, trackingMode, 
                    //     entitySet, entityChangedCallback, entityCollectionChangedCallback)
                    result = Expression.New(
                        constructorInfo,
                        this.materializerExpression, 
                        result.Arguments[0],   // context
                        result.Arguments[1],   // items 
                        result.Arguments[2],   // trackingMode 
                        result.Arguments[3],   // entityset name
                        result.Arguments[4],   // entity changed cb 
                        result.Arguments[5]);  // collection changed cb
                }
            }
 
            if (annotation != null)
            { 
                // Propagate the annotation of the "items" parameter (the enumerable) as the annotation 
                //   of the DataServiceCollection, since it now represents the same thing.
                this.annotations.Add(result, annotation); 
            }

            // Note that we end up just falling through without changing anything for
            // constructors of DataServiceCollection that aren't correlated (i.e. don't take 
            // an enumerable in the input).
            return result; 
        } 

        /// Rewrites a call to Select() by adding to the current paths to project out. 
        /// Call expression.
        /// Expression with annotated path to include in member binding.
        private Expression RebindMethodCallForMemberSelect(MethodCallExpression call)
        { 
            Debug.Assert(call != null, "call != null");
            Debug.Assert(call.Method.Name == "Select", "call.Method.Name == 'Select'"); 
            Debug.Assert(call.Object == null, "call.Object == null -- otherwise this isn't a call to a static Select method"); 
            Debug.Assert(call.Arguments.Count == 2, "call.Arguments.Count == 2 -- otherwise this isn't the expected Select() call on IQueryable");
 
            // Get the path for the parameter value that will be used in the Select lambda.
            Expression result = null;
            Expression parameterSource = this.Visit(call.Arguments[0]);
            ExpressionAnnotation annotation; 
            this.annotations.TryGetValue(parameterSource, out annotation);
 
            // It's possibly that we haven't annotated this argument, in which 
            // case we don't care about this select, eg new { a = new int[] {1}.Select(i=>i+1).First() }
            if (annotation != null) 
            {
                // With this information from an annotation:
                // {t->*.Players}
                // 
                // Call this lambda:
                // {(mat, entry1, type1) => Convert(ProjectionValueForPath(mat, entry1, type1, p->*.FirstName))} 
                // 
                // Annotating the intermediate expressions so we mark them
                // as rewrites and we know to route them through materializer 
                // helpers, eg to preserve paging information.
                Expression selectorExpression = this.Visit(call.Arguments[1]);
                Type returnElementType = call.Method.ReturnType.GetGenericArguments()[0];
                result = CallMaterializer( 
                    "ProjectionSelect",
                    this.materializerExpression, 
                    this.pathBuilder.ParameterEntryInScope, 
                    this.pathBuilder.ExpectedParamTypeInScope,
                    Expression.Constant(returnElementType, typeof(Type)), 
                    Expression.Constant(annotation.Segment.StartPath, typeof(object)),
                    selectorExpression);
                this.annotations.Add(result, annotation);
                result = CallMaterializerWithType( 
                    "EnumerateAsElementType",
                    new Type[] { returnElementType }, 
                    result); 
                this.annotations.Add(result, annotation);
            } 

            if (result == null)
            {
                result = base.VisitMethodCall(call); 
            }
 
            return result; 
        }
 
        /// Rewrites a call to ToList in the specified method.
        /// Call expression.
        /// Expression with annotated path to include in member binding.
        ///  
        /// All that is necessary here is to rewrite the call to Select() and indicate
        /// that the target type is a given List<T>. 
        /// 
        ///
 
        private Expression RebindMethodCallForMemberToList(MethodCallExpression call)
        {
            Debug.Assert(call != null, "call != null");
            Debug.Assert(call.Object == null, "call.Object == null -- otherwise this isn't a call to a static ToList method"); 
            Debug.Assert(call.Method.Name == "ToList", "call.Method.Name == 'ToList'");
 
            // Wrap the source to the .ToList() call if it's our rewrite. 
            Debug.Assert(call.Arguments.Count == 1, "call.Arguments.Count == 1 -- otherwise this isn't the expected ToList() call on IEnumerable");
 
            Expression result = this.Visit(call.Arguments[0]);
            ExpressionAnnotation annotation;
            if (this.annotations.TryGetValue(result, out annotation))
            { 
                result = this.TypedEnumerableToList(result, call.Type);
                this.annotations.Add(result, annotation); 
            } 

            return result; 
        }

        /// Rewrites a method call used in a sequence method (possibly over entity types).
        /// Call expression. 
        /// Expression that can be called directly to yield the expected value.
        private Expression RebindMethodCallForNewSequence(MethodCallExpression call) 
        { 
            Debug.Assert(call != null, "call != null");
            Debug.Assert(ProjectionAnalyzer.IsMethodCallAllowedEntitySequence(call), "ProjectionAnalyzer.IsMethodCallAllowedEntitySequence(call)"); 
            Debug.Assert(call.Object == null, "call.Object == null -- otherwise this isn't the supported Select or ToList methods");

            // The only expressions that require rewriting are:
            // - [null].Select(entity-type-based-expression, lambda) 
            // - [null].ToList(entity-type-enumeration)
            // 
            // All other expressions can be visited normally. 
            Expression result = null;
 
            if (call.Method.Name == "Select")
            {
                Debug.Assert(call.Arguments.Count == 2, "call.Arguments.Count == 2 -- otherwise this isn't the argument we expected");
 
                // Get the path for the parameter value that will be used in the Select lambda.
                Expression parameterSource = this.Visit(call.Arguments[0]); 
                ExpressionAnnotation annotation; 
                this.annotations.TryGetValue(parameterSource, out annotation);
 
                // It's possibly that we haven't annotated this argument, in which
                // case we don't care about this select, eg new { a = new int[] {1}.Select(i=>i+1).First() }
                if (annotation != null)
                { 
                    // With this information from an annotation:
                    // {t->*.Players} 
                    // 
                    // Call this lambda:
                    // {(mat, entry1, type1) => Convert(ProjectionValueForPath(mat, entry1, type1, p->*.FirstName))} 
                    Expression selectorExpression = this.Visit(call.Arguments[1]);
                    Type returnElementType = call.Method.ReturnType.GetGenericArguments()[0];
                    result = CallMaterializer(
                        "ProjectionSelect", 
                        this.materializerExpression,
                        this.pathBuilder.ParameterEntryInScope, 
                        this.pathBuilder.ExpectedParamTypeInScope, 
                        Expression.Constant(returnElementType, typeof(Type)),
                        Expression.Constant(annotation.Segment.StartPath, typeof(object)), 
                        selectorExpression);
                    this.annotations.Add(result, annotation);
                    result = CallMaterializerWithType(
                        "EnumerateAsElementType", 
                        new Type[] { returnElementType },
                        result); 
                    this.annotations.Add(result, annotation); 
                }
            } 
            else
            {
                Debug.Assert(call.Method.Name == "ToList", "call.Method.Name == 'ToList'");
 
                // Annotate the source to the .ToList() call.
                Expression source = this.Visit(call.Arguments[0]); 
                ExpressionAnnotation annotation; 
                if (this.annotations.TryGetValue(source, out annotation))
                { 
                    result = this.TypedEnumerableToList(source, call.Type);
                    this.annotations.Add(result, annotation);
                }
            } 

            if (result == null) 
            { 
                result = base.VisitMethodCall(call);
            } 

            return result;
        }
 
        /// Returns a method call that returns a list from a typed enumerable.
        /// Expression to convert. 
        /// Target type to return. 
        /// The new expression.
        private Expression TypedEnumerableToList(Expression source, Type targetType) 
        {
            Debug.Assert(source != null, "source != null");
            Debug.Assert(targetType != null, "targetType != null");
 
            //
 
            Type enumeratedType = source.Type.GetGenericArguments()[0]; 
            Type listElementType = targetType.GetGenericArguments()[0];
 
            // Return the annotated expression.
            Expression result = CallMaterializerWithType(
                "ListAsElementType",
                new Type[] { enumeratedType, listElementType }, 
                this.materializerExpression,
                source); 
 
            return result;
        } 

        #endregion Private methods.

        #region Inner types. 

        /// Annotates an expression, typically from the target tree. 
        internal class ExpressionAnnotation 
        {
            /// Segment that marks the path found to an expression. 
            internal ProjectionPathSegment Segment
            {
                get;
                set; 
            }
        } 
 
        #endregion Inner types.
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//  
// Provides a class that can create a materialization plan for a
// projection. 
//  
//---------------------------------------------------------------------
 
//// Uncomment the following line to trace projection building activity.
////#define TRACE_CLIENT_PROJECTIONS

namespace System.Data.Services.Client 
{
    #region Namespaces. 
 
    using System;
    using System.Collections.Generic; 
    using System.Diagnostics;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection; 

    #endregion Namespaces. 
 
    /// 
    /// Use this class to create a  for a given projection lambda. 
    /// 
    internal class ProjectionPlanCompiler : ALinqExpressionVisitor
    {
        #region Private fields. 

        /// Annotations being tracked on this tree. 
        private readonly Dictionary annotations; 

        /// Expression that refers to the materializer. 
        private readonly ParameterExpression materializerExpression;

        /// Tracks rewrite-to-source rewrites introduced by expression normalizer.
        private readonly Dictionary normalizerRewrites; 

        /// Number to suffix to identifiers to help with debugging. 
        private int identifierId; 

        /// Path builder used to help with tracking state while compiling. 
        private ProjectionPathBuilder pathBuilder;

        /// Whether the top level projection has been found.
        private bool topLevelProjectionFound; 

        #endregion Private fields. 
 
        #region Constructors.
 
        /// 
        /// Initializes a new  instance.
        /// 
        /// Rewrites introduces by normalizer. 
        private ProjectionPlanCompiler(Dictionary normalizerRewrites)
        { 
            this.annotations = new Dictionary(ReferenceEqualityComparer.Instance); 
            this.materializerExpression = Expression.Parameter(typeof(object), "mat");
            this.normalizerRewrites = normalizerRewrites; 
            this.pathBuilder = new ProjectionPathBuilder();
        }

        #endregion Constructors. 

        #region Internal methods. 
 
        /// Creates a projection plan from the specified .
        /// Projection expression. 
        /// Tracks rewrite-to-source rewrites introduced by expression normalizer.
        /// A new  instance.
        internal static ProjectionPlan CompilePlan(LambdaExpression projection, Dictionary normalizerRewrites)
        { 
            Debug.Assert(projection != null, "projection != null");
            Debug.Assert(projection.Parameters.Count == 1, "projection.Parameters.Count == 1"); 
            Debug.Assert( 
                projection.Body.NodeType == ExpressionType.Constant ||
                projection.Body.NodeType == ExpressionType.MemberInit || 
                projection.Body.NodeType == ExpressionType.MemberAccess ||
                projection.Body.NodeType == ExpressionType.Convert ||
                projection.Body.NodeType == ExpressionType.ConvertChecked ||
                projection.Body.NodeType == ExpressionType.New, 
                "projection.Body.NodeType == Constant, MemberInit, MemberAccess, Convert(Checked) New");
 
            ProjectionPlanCompiler rewriter = new ProjectionPlanCompiler(normalizerRewrites); 
#if TRACE_CLIENT_PROJECTIONS
            Trace.WriteLine("Projection: " + projection); 
#endif

            Expression plan = rewriter.Visit(projection);
#if TRACE_CLIENT_PROJECTIONS 
            Trace.WriteLine("Becomes: " + plan);
#endif 
 
            ProjectionPlan result = new ProjectionPlan();
            result.Plan = (Func)((LambdaExpression)plan).Compile(); 
            result.ProjectedType = projection.Body.Type;
#if DEBUG
            result.SourceProjection = projection;
            result.TargetProjection = plan; 
#endif
            return result; 
        } 

        /// Binary visit method. 
        /// Binary expression to visit.
        /// (Possibly rewritten) binary expression.
        /// 
        /// This override is introduced because binary expressions are one of 
        /// the scopes at which normalization happens.
        ///  
        internal override Expression VisitBinary(BinaryExpression b) 
        {
            Expression original = this.GetExpressionBeforeNormalization(b); 
            if (original == b)
            {
                return base.VisitBinary(b);
            } 
            else
            { 
                return this.Visit(original); 
            }
        } 

        /// Visits the specified  expression.
        /// Expression to check.
        /// The visited expression. 
        /// 
        /// This override allows us to check for rewrites created by 
        /// ExpressionNormalizer.CreateCompareExpression. 
        /// 
        internal override Expression VisitConditional(ConditionalExpression conditional) 
        {
            Debug.Assert(conditional != null, "conditional != null");
            Expression original = this.GetExpressionBeforeNormalization(conditional);
            if (original != conditional) 
            {
                return this.Visit(original); 
            } 

            var nullCheck = ResourceBinder.PatternRules.MatchNullCheck(this.pathBuilder.LambdaParameterInScope, conditional); 
            if (!nullCheck.Match || !ClientType.CheckElementTypeIsEntity(nullCheck.AssignExpression.Type))
            {
                return base.VisitConditional(conditional);
            } 

            return this.RebindConditionalNullCheck(conditional, nullCheck); 
        } 

        /// Unary visit method. 
        /// Unary expression to visit.
        /// (Possibly rewritten) unary expression.
        /// 
        /// This override is introduced because unary expressions are one of 
        /// the scopes at which normalization happens.
        ///  
        internal override Expression VisitUnary(UnaryExpression u) 
        {
            Expression original = this.GetExpressionBeforeNormalization(u); 
            Expression result;
            if (original == u)
            {
                result = base.VisitUnary(u); 
                UnaryExpression unaryResult = result as UnaryExpression;
                if (unaryResult != null) 
                { 
                    ExpressionAnnotation annotation;
                    if (this.annotations.TryGetValue(unaryResult.Operand, out annotation)) 
                    {
                        this.annotations[result] = annotation;
                    }
                } 
            }
            else 
            { 
                result = this.Visit(original);
            } 

            return result;
        }
 
        /// 
        /// MemberExpression visit method 
        ///  
        /// The MemberExpression expression to visit
        /// The visited MemberExpression expression  
        internal override Expression VisitMemberAccess(MemberExpression m)
        {
            Debug.Assert(m != null, "m != null");
 
            Expression result;
            Expression baseSourceExpression = m.Expression; 
 
            // if primitive or nullable primitive, allow member access... i.e. calling Value on nullable
            if (ClientConvert.IsKnownNullableType(baseSourceExpression.Type)) 
            {
                result = base.VisitMemberAccess(m);
            }
            else 
            {
                Expression baseTargetExpression = this.Visit(baseSourceExpression); 
                ExpressionAnnotation annotation; 
                if (this.annotations.TryGetValue(baseTargetExpression, out annotation))
                { 
                    result = this.RebindMemberAccess(m, annotation);
                }
                else
                { 
                    result = Expression.MakeMemberAccess(baseTargetExpression, m.Member);
                } 
            } 

            return result; 
        }

        /// Parameter visit method.
        /// Parameter to visit. 
        /// Resulting expression.
        ///  
        /// The parameter may get rewritten as a materializing projection if 
        /// it refers to an entity outside of member binding. In this case,
        /// it becomes a standalone tracked entity. 
        /// 
        internal override Expression VisitParameter(ParameterExpression p)
        {
            Debug.Assert(p != null, "p != null"); 

            // If this parameter isn't interesting, we're not doing any rewrites. 
            Expression result; 
            ExpressionAnnotation annotation;
            if (this.annotations.TryGetValue(p, out annotation)) 
            {
                result = this.RebindParameter(p, annotation);
            }
            else 
            {
                result = base.VisitParameter(p); 
            } 

            return result; 
        }

        /// 
        /// MemberInitExpression visit method 
        /// 
        /// The MemberInitExpression to visit 
        /// The visited MemberInitExpression 
        /// 
        /// A MemberInitExpression on a knownEntityType implies that we 
        /// want to materialize the thing.
        /// 
        internal override Expression VisitMemberInit(MemberInitExpression init)
        { 
            this.pathBuilder.EnterMemberInit(init);
 
            Expression result = null; 
            if (this.pathBuilder.CurrentIsEntity && init.Bindings.Count > 0)
            { 
                result = this.RebindEntityMemberInit(init);
            }
            else
            { 
                result = base.VisitMemberInit(init);
            } 
 
            this.pathBuilder.LeaveMemberInit();
            return result; 
        }

        /// Visits a method call expression.
        /// Expression to visit. 
        /// A (possibly rewritten) expression for .
        internal override Expression VisitMethodCall(MethodCallExpression m) 
        { 
            Debug.Assert(m != null, "m != null");
 
            Expression original = this.GetExpressionBeforeNormalization(m);
            if (original != m)
            {
                return this.Visit(original); 
            }
 
            Expression result; 
            if (this.pathBuilder.CurrentIsEntity)
            { 
                Debug.Assert(
                    ProjectionAnalyzer.IsMethodCallAllowedEntitySequence(m) || ResourceBinder.PatternRules.MatchReferenceEquals(m),
                    "ProjectionAnalyzer.IsMethodCallAllowedEntitySequence(m) || ResourceBinder.PatternRules.MatchReferenceEquals(m) -- otherwise ProjectionAnalyzer should have blocked this for entities");
                if (m.Method.Name == "Select") 
                {
                    result = this.RebindMethodCallForMemberSelect(m); 
                } 
                else if (m.Method.Name == "ToList")
                { 
                    result = this.RebindMethodCallForMemberToList(m);
                }
                else
                { 
                    Debug.Assert(m.Method.Name == "ReferenceEquals", "We don't know how to handle this method, ProjectionAnalyzer updated?");
                    result = base.VisitMethodCall(m); 
                } 
            }
            else 
            {
                if (ProjectionAnalyzer.IsMethodCallAllowedEntitySequence(m))
                {
                    result = this.RebindMethodCallForNewSequence(m); 
                }
                else 
                { 
                    result = base.VisitMethodCall(m);
                } 
            }

            return result;
        } 

        /// Visits a new expression 
        /// Expression to visit 
        /// A (possibly rewritten) expression for .
        internal override NewExpression VisitNew(NewExpression nex) 
        {
            Debug.Assert(nex != null, "nex != null");

            // Special case DataServiceCollection creation so context instance 
            // and paging (continuations) propperly flow through
            if (ResourceBinder.PatternRules.MatchNewDataServiceCollectionOfT(nex)) 
            { 
                return this.RebindNewExpressionForDataServiceCollectionOfT(nex);
            } 

            return base.VisitNew(nex);
        }
 
        /// LambdaExpression visit method.
        /// The LambdaExpression to visit 
        /// The visited LambdaExpression 
        internal override Expression VisitLambda(LambdaExpression lambda)
        { 
            Debug.Assert(lambda != null, "lambda != null");

            Expression result;
            if (!this.topLevelProjectionFound || lambda.Parameters.Count == 1 && ClientType.CheckElementTypeIsEntity(lambda.Parameters[0].Type)) 
            {
                this.topLevelProjectionFound = true; 
 
                ParameterExpression expectedTypeParameter = Expression.Parameter(typeof(Type), "type" + this.identifierId);
                ParameterExpression entryParameter = Expression.Parameter(typeof(object), "entry" + this.identifierId); 
                this.identifierId++;

                this.pathBuilder.EnterLambdaScope(lambda, entryParameter, expectedTypeParameter);
                ProjectionPath parameterPath = new ProjectionPath(lambda.Parameters[0], expectedTypeParameter, entryParameter); 
                ProjectionPathSegment parameterSegment = new ProjectionPathSegment(parameterPath, null, null);
                parameterPath.Add(parameterSegment); 
                this.annotations[lambda.Parameters[0]] = new ExpressionAnnotation() { Segment = parameterSegment }; 

                Expression body = this.Visit(lambda.Body); 

                // Value types must be boxed explicitly; the lambda initialization
                // won't do it for us (type-compatible types still work, so all
                // references will work fine with System.Object). 
                if (body.Type.IsValueType)
                { 
                    body = Expression.Convert(body, typeof(object)); 
                }
 
                result = Expression.Lambda>(
                    body,
                    this.materializerExpression,
                    entryParameter, 
                    expectedTypeParameter);
 
                this.pathBuilder.LeaveLambdaScope(); 
            }
            else 
            {
                result = base.VisitLambda(lambda);
            }
 
            return result;
        } 
 
        #endregion Internal methods.
 
        #region Private methods.

        /// Generates a call to a static method on AtomMaterializer.
        /// Name of method to invoke. 
        /// Arguments to pass to method.
        /// The constructed expression. 
        ///  
        /// There is no support for overload resolution - method names in AtomMaterializer
        /// must be unique. 
        /// 
        private static Expression CallMaterializer(string methodName, params Expression[] arguments)
        {
            return CallMaterializerWithType(methodName, null, arguments); 
        }
 
        /// Generates a call to a static method on AtomMaterializer. 
        /// Name of method to invoke.
        /// Type arguments for method (possibly null). 
        /// Arguments to pass to method.
        /// The constructed expression.
        /// 
        /// There is no support for overload resolution - method names in AtomMaterializer 
        /// must be unique.
        ///  
        private static Expression CallMaterializerWithType(string methodName, Type[] typeArguments, params Expression[] arguments) 
        {
            Debug.Assert(methodName != null, "methodName != null"); 
            Debug.Assert(arguments != null, "arguments != null");

            MethodInfo method = typeof(AtomMaterializerInvoker).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
            Debug.Assert(method != null, "method != null - found " + methodName); 
            if (typeArguments != null)
            { 
                method = method.MakeGenericMethod(typeArguments); 
            }
 
            return Expression.Call(method, arguments);
        }

        /// Creates an expression that calls ProjectionCheckValueForPathIsNull. 
        /// Expression for root entry for paths.
        /// Expression for expected type for entry. 
        /// Path to check null value for. 
        /// A new expression with the call instance.
        private Expression CallCheckValueForPathIsNull(Expression entry, Expression entryType, ProjectionPath path) 
        {
            Expression result = CallMaterializer("ProjectionCheckValueForPathIsNull", entry, entryType, Expression.Constant(path, typeof(object)));
            this.annotations.Add(result, new ExpressionAnnotation() { Segment = path[path.Count - 1] });
            return result; 
        }
 
        /// Creates an expression that calls ProjectionValueForPath. 
        /// Expression for root entry for paths.
        /// Expression for expected type for entry. 
        /// Path to pull value from.
        /// A new expression with the call instance.
        private Expression CallValueForPath(Expression entry, Expression entryType, ProjectionPath path)
        { 
            Debug.Assert(entry != null, "entry != null");
            Debug.Assert(path != null, "path != null"); 
 
            Expression result = CallMaterializer("ProjectionValueForPath", this.materializerExpression, entry, entryType, Expression.Constant(path, typeof(object)));
            this.annotations.Add(result, new ExpressionAnnotation() { Segment = path[path.Count - 1] }); 
            return result;
        }

        /// Creates an expression that calls ProjectionValueForPath. 
        /// Expression for root entry for paths.
        /// Expression for expected type for entry. 
        /// Path to pull value from. 
        /// Path to convert result for.
        /// A new expression with the call instance. 
        private Expression CallValueForPathWithType(Expression entry, Expression entryType, ProjectionPath path, Type type)
        {
            Debug.Assert(entry != null, "entry != null");
            Debug.Assert(path != null, "path != null"); 

            Expression value = this.CallValueForPath(entry, entryType, path); 
            Expression result = Expression.Convert(value, type); 
            this.annotations.Add(result, new ExpressionAnnotation() { Segment = path[path.Count - 1] });
            return result; 
        }

        /// 
        /// Rebinds a conditional that performs a null check on an entity. 
        /// 
        /// Conditional expression. 
        /// Results of null check analysis. 
        /// The rebound expression.
        ///  
        /// Do a rewrite to avoid creating a type in the null check:
        ///   a.b == null ? null : [a.b]-based expression
        /// becomes
        ///   ProjectionIsNull(a.b) ? null : [a.b]-based expression 
        /// 
        private Expression RebindConditionalNullCheck(ConditionalExpression conditional, ResourceBinder.PatternRules.MatchNullCheckResult nullCheck) 
        { 
            Debug.Assert(conditional != null, "conditional != null");
            Debug.Assert(nullCheck.Match, "nullCheck.Match -- otherwise no reason to call this rebind method"); 

            Expression testToNullForProjection = this.Visit(nullCheck.TestToNullExpression);
            Expression assignForProjection = this.Visit(nullCheck.AssignExpression);
            ExpressionAnnotation testToNullAnnotation; 
            if (!this.annotations.TryGetValue(testToNullForProjection, out testToNullAnnotation))
            { 
                return base.VisitConditional(conditional); 
            }
 
            ProjectionPathSegment testToNullSegment = testToNullAnnotation.Segment;

            Expression testToNullThroughMethod = this.CallCheckValueForPathIsNull(
                testToNullSegment.StartPath.RootEntry, 
                testToNullSegment.StartPath.ExpectedRootType,
                testToNullSegment.StartPath); 
 
            Expression test = testToNullThroughMethod;
            Expression iftrue = Expression.Constant(null, assignForProjection.Type); 
            Expression iffalse = assignForProjection;
            Expression result = Expression.Condition(test, iftrue, iffalse);
            return result;
        } 

        ///  
        /// Rebinds the specified  expression by gathering 
        /// annotated paths and returning an expression that calls the
        /// ProjectionGetEntity method. 
        /// 
        /// Member initialization expression.
        /// A new expression suitable for materialization.
        private Expression RebindEntityMemberInit(MemberInitExpression init) 
        {
            Debug.Assert(init != null, "init != null"); 
            Debug.Assert(init.Bindings.Count > 0, "init.Bindings.Count > 0 -- otherwise this is just empty construction"); 

            // We "jump" into entities only if we're not already materializing an entity. 
            Expression[] expressions;
            if (!this.pathBuilder.HasRewrites)
            {
                MemberAssignmentAnalysis propertyAnalysis = MemberAssignmentAnalysis.Analyze( 
                    this.pathBuilder.LambdaParameterInScope,
                    ((MemberAssignment)init.Bindings[0]).Expression); 
                expressions = propertyAnalysis.GetExpressionsToTargetEntity(); 
                Debug.Assert(expressions.Length != 0, "expressions.Length != 0 -- otherwise there is no correlation to parameter in entity member init");
            } 
            else
            {
                expressions = MemberAssignmentAnalysis.EmptyExpressionArray;
            } 

            Expression entryParameterAtMemberInit = this.pathBuilder.ParameterEntryInScope; 
            List propertyNames = new List(); 
            List> propertyFunctions = new List>();
            Type projectedType = init.NewExpression.Type; 
            Expression projectedTypeExpression = Expression.Constant(projectedType, typeof(Type));

            // We may need to materialize from deeper in the entity tree for anonymous types.
            // t => new { nested = new Nested() { nid = t.nested.nid } 
            //
            // We do the same kind of rewriting we'd do for a nested entity 
            // but at the initializing scope (rather than at the member assignment scope). 
            //
            // t=> new { nested = ProjInit(GetEntry(entry0, "Nested"), "nid", *->nid) } 
            Expression entryToInitValue;    // Expression that yields value for entry in target tree.
            Expression expectedParamValue;  // Expression that yield expectedType in target tree.
            ParameterExpression entryParameterForMembers;       // Parameter expression members think of as "entry".
            ParameterExpression expectedParameterForMembers;    // Parameter expression members think of as "expectedType" for entry. 
            string[] expressionNames = expressions.Skip(1).Select(e => ((MemberExpression)e).Member.Name).ToArray();
            if (expressions.Length <= 1) 
            { 
                entryToInitValue = this.pathBuilder.ParameterEntryInScope;
                expectedParamValue = this.pathBuilder.ExpectedParamTypeInScope; 
                entryParameterForMembers = (ParameterExpression)this.pathBuilder.ParameterEntryInScope;
                expectedParameterForMembers = (ParameterExpression)this.pathBuilder.ExpectedParamTypeInScope;
            }
            else 
            {
                entryToInitValue = this.GetDeepestEntry(expressions); 
                expectedParamValue = projectedTypeExpression; 
                entryParameterForMembers = Expression.Parameter(typeof(object), "subentry" + this.identifierId++);
                expectedParameterForMembers = (ParameterExpression)this.pathBuilder.ExpectedParamTypeInScope; 

                // Annotate the entry expression with 'how we get to it' information.
                // The annotation on entryToInitiValue is picked up
                // The annotation on entryParameterForMembers is picked up to build nested member-init on entities. 
                ProjectionPath entryPath = new ProjectionPath(
                    (ParameterExpression)this.pathBuilder.LambdaParameterInScope, 
                    this.pathBuilder.ExpectedParamTypeInScope, 
                    this.pathBuilder.ParameterEntryInScope,
                    expressions.Skip(1)); 

                this.annotations.Add(entryToInitValue, new ExpressionAnnotation() { Segment = entryPath[entryPath.Count - 1] });
                this.annotations.Add(entryParameterForMembers, new ExpressionAnnotation() { Segment = entryPath[entryPath.Count - 1] });
                this.pathBuilder.RegisterRewrite(this.pathBuilder.LambdaParameterInScope, expressionNames, entryParameterForMembers); 
            }
 
            for (int i = 0; i < init.Bindings.Count; i++) 
            {
                MemberAssignment assignment = (MemberAssignment)init.Bindings[i]; 
                propertyNames.Add(assignment.Member.Name);

                LambdaExpression propertyLambda;
 
                // Here are the rewrites we do for member inits:
                // new T { id = t.id } 
                // => ProjInit(pt, "id", f(t -> *.id)); 
                //
                // new T { t2 = new T2 { id2 = *.t2.id2 } } 
                // => ProjInit(pt, "t2", f(ProjInit(pt->t2), "id2", *.id2)))
                if ((ClientType.CheckElementTypeIsEntity(assignment.Member.ReflectedType) &&
                     assignment.Expression.NodeType == ExpressionType.MemberInit))
                { 
                    Expression nestedEntry = CallMaterializer(
                        "ProjectionGetEntry", 
                        entryParameterAtMemberInit, 
                        Expression.Constant(assignment.Member.Name, typeof(string)));
                    ParameterExpression nestedEntryParameter = Expression.Parameter( 
                        typeof(object),
                        "subentry" + this.identifierId++);

                    // Register the rewrite from the top to the entry if necessary. 
                    ProjectionPath entryPath;
                    ExpressionAnnotation entryAnnotation; 
                    if (this.annotations.TryGetValue(this.pathBuilder.ParameterEntryInScope, out entryAnnotation)) 
                    {
                        entryPath = new ProjectionPath( 
                            (ParameterExpression)this.pathBuilder.LambdaParameterInScope,
                            this.pathBuilder.ExpectedParamTypeInScope,
                            entryParameterAtMemberInit);
                        entryPath.AddRange(entryAnnotation.Segment.StartPath); 
                    }
                    else 
                    { 
                        entryPath = new ProjectionPath(
                            (ParameterExpression)this.pathBuilder.LambdaParameterInScope, 
                            this.pathBuilder.ExpectedParamTypeInScope,
                            entryParameterAtMemberInit,
                            expressions.Skip(1));
                    } 

                    ProjectionPathSegment nestedSegment = new ProjectionPathSegment( 
                        entryPath, 
                        assignment.Member.Name,
                        assignment.Member.ReflectedType); 

                    entryPath.Add(nestedSegment);

                    string[] names = (entryPath.Where(m => m.Member != null).Select(m => m.Member)).ToArray(); 

                    this.annotations.Add(nestedEntryParameter, new ExpressionAnnotation() { Segment = nestedSegment }); 
                    this.pathBuilder.RegisterRewrite(this.pathBuilder.LambdaParameterInScope, names, nestedEntryParameter); 
                    Expression e = this.Visit(assignment.Expression);
                    this.pathBuilder.RevokeRewrite(this.pathBuilder.LambdaParameterInScope, names); 
                    this.annotations.Remove(nestedEntryParameter);

                    e = Expression.Convert(e, typeof(object));
                    ParameterExpression[] parameters = 
                        new ParameterExpression[]
                        { 
                            this.materializerExpression, 
                            nestedEntryParameter,
                            expectedParameterForMembers, 
                        };
                    propertyLambda = Expression.Lambda(e, parameters);

                    Expression[] nestedParams = 
                        new Expression[]
                        { 
                            this.materializerExpression, 
                            nestedEntry,
                            expectedParameterForMembers, 
                        };
                    var invokeParameters =
                        new ParameterExpression[]
                        { 
                            this.materializerExpression,
                            (ParameterExpression)entryParameterAtMemberInit, 
                            expectedParameterForMembers, 
                        };
                    propertyLambda = Expression.Lambda(Expression.Invoke(propertyLambda, nestedParams), invokeParameters); 
                }
                else
                {
                    // We need an expression of object, which might require boxing. 
                    Expression e = this.Visit(assignment.Expression);
                    e = Expression.Convert(e, typeof(object)); 
                    ParameterExpression[] parameters = 
                        new ParameterExpression[]
                        { 
                            this.materializerExpression,
                            entryParameterForMembers,
                            expectedParameterForMembers,
                        }; 
                    propertyLambda = Expression.Lambda(e, parameters);
                } 
 
#if TRACE_CLIENT_PROJECTIONS
                Trace.WriteLine("Compiling lambda for " + assignment.Member.Name + ": " + propertyLambda); 
#endif
                propertyFunctions.Add((Func) propertyLambda.Compile());
            }
 
            // Revoke rewrites used for nested initialization.
            for (int i = 1; i < expressions.Length; i++) 
            { 
                this.pathBuilder.RevokeRewrite(this.pathBuilder.LambdaParameterInScope, expressionNames);
                this.annotations.Remove(entryToInitValue); 
                this.annotations.Remove(entryParameterForMembers);
            }

            Expression reboundExpression = CallMaterializer( 
                "ProjectionInitializeEntity",
                this.materializerExpression, 
                entryToInitValue, 
                expectedParamValue,
                projectedTypeExpression, 
                Expression.Constant(propertyNames.ToArray()),
                Expression.Constant(propertyFunctions.ToArray()));

            return Expression.Convert(reboundExpression, projectedType); 
        }
 
        ///  
        /// Creates an expression that gets the deepest entry that will be found on the
        /// specified  (for the target tree). 
        /// 
        /// Path of expressions to walk.
        /// An expression that invokes ProjectionGetEntry on the target tree.
        private Expression GetDeepestEntry(Expression[] path) 
        {
            Debug.Assert(path.Length > 1, "path.Length > 1"); 
 
            Expression result = null;
            int pathIndex = 1; 
            do
            {
                result = CallMaterializer(
                    "ProjectionGetEntry", 
                    result ?? this.pathBuilder.ParameterEntryInScope,
                    Expression.Constant(((MemberExpression)path[pathIndex]).Member.Name, typeof(string))); 
                pathIndex++; 
            }
            while (pathIndex < path.Length); 

            return result;
        }
 
        /// Gets an expression before its rewrite.
        /// Expression to check. 
        /// The expression before normalization. 
        private Expression GetExpressionBeforeNormalization(Expression expression)
        { 
            Debug.Assert(expression != null, "expression != null");
            if (this.normalizerRewrites != null)
            {
                Expression original; 
                if (this.normalizerRewrites.TryGetValue(expression, out original))
                { 
                    expression = original; 
                }
            } 

            return expression;
        }
 
        /// Rebinds the specified parameter expression as a path-based access.
        /// Expression to rebind. 
        /// Annotation for the expression to rebind. 
        /// The rebound expression.
        private Expression RebindParameter(Expression expression, ExpressionAnnotation annotation) 
        {
            Debug.Assert(expression != null, "expression != null");
            Debug.Assert(annotation != null, "annotation != null");
 
            Expression result;
            result = this.CallValueForPathWithType( 
                annotation.Segment.StartPath.RootEntry, 
                annotation.Segment.StartPath.ExpectedRootType,
                annotation.Segment.StartPath, 
                expression.Type);

            // Refresh the annotation so the next one that comes along
            // doesn't start off with an already-written path. 
            ProjectionPath parameterPath = new ProjectionPath(
                annotation.Segment.StartPath.Root, 
                annotation.Segment.StartPath.ExpectedRootType, 
                annotation.Segment.StartPath.RootEntry);
            ProjectionPathSegment parameterSegment = new ProjectionPathSegment(parameterPath, null, null); 
            parameterPath.Add(parameterSegment);
            this.annotations[expression] = new ExpressionAnnotation() { Segment = parameterSegment };

            return result; 
        }
 
        /// Rebinds the specified member access expression into a path-based value retrieval method call. 
        /// Member expression.
        /// Annotation for the base portion of the expression. 
        /// A rebound expression.
        private Expression RebindMemberAccess(MemberExpression m, ExpressionAnnotation baseAnnotation)
        {
            Debug.Assert(m != null, "m != null"); 
            Debug.Assert(baseAnnotation != null, "baseAnnotation != null");
 
            ProjectionPathSegment memberSegment; 

            // If we are in nested member-init, we rewrite the property 
            // accessors that are in the form of top.nested.id to
            // nested.id.
            Expression baseSourceExpression = m.Expression;
            Expression result = this.pathBuilder.GetRewrite(baseSourceExpression); 
            if (result != null)
            { 
                Expression baseTypeExpression = Expression.Constant(baseSourceExpression.Type, typeof(Type)); 
                ProjectionPath nestedPath = new ProjectionPath(result as ParameterExpression, baseTypeExpression, result);
                ProjectionPathSegment nestedSegment = new ProjectionPathSegment(nestedPath, m.Member.Name, m.Type); 
                nestedPath.Add(nestedSegment);
                result = this.CallValueForPathWithType(result, baseTypeExpression, nestedPath, m.Type);
            }
            else 
            {
                // This actually modifies the path for the underlying 
                // segments, but that shouldn't be a problem. Actually 
                // we should be able to remove it from the dictionary.
                // There should be no aliasing problems, because 
                // annotations always come from target expression
                // that are generated anew (except parameters,
                // but those)
                memberSegment = new ProjectionPathSegment(baseAnnotation.Segment.StartPath, m.Member.Name, m.Type); 
                baseAnnotation.Segment.StartPath.Add(memberSegment);
                result = this.CallValueForPathWithType( 
                    baseAnnotation.Segment.StartPath.RootEntry, 
                    baseAnnotation.Segment.StartPath.ExpectedRootType,
                    baseAnnotation.Segment.StartPath, 
                    m.Type);
            }

            return result; 
        }
 
        /// Rewrites a new statement for DataServiceCollection so the paging information 
        /// is preserved in the materializer.
        /// NewExpression to create a collection 
        /// The rewritten expression.
        private NewExpression RebindNewExpressionForDataServiceCollectionOfT(NewExpression nex)
        {
            Debug.Assert(nex != null, "nex != null"); 
            Debug.Assert(
                ResourceBinder.PatternRules.MatchNewDataServiceCollectionOfT(nex), 
                "Called should have checked that the 'new' was for our collection type"); 

            NewExpression result = base.VisitNew(nex); 

            ExpressionAnnotation annotation = null;

            if (result != null) 
            {
                ConstructorInfo constructorInfo = 
                    nex.Type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First( 
                        c => c.GetParameters().Length == 7 && c.GetParameters()[0].ParameterType == typeof(object));
 
                Type enumerable = typeof(IEnumerable<>).MakeGenericType(nex.Type.GetGenericArguments()[0]);

                if (result.Arguments.Count == 1 && result.Constructor == nex.Type.GetConstructor(new[] { enumerable }) &&
                    this.annotations.TryGetValue(result.Arguments[0], out annotation)) 
                {
                    // DataServiceCollection( 
                    //     IEnumerable items) 
                    // ->
                    // DataServiceCollection(materializer, null, items, TrackingMode.AutoChangeTracking, null, null, null) 
                    result = Expression.New(
                        constructorInfo,
                        this.materializerExpression,
                        Expression.Constant(null, typeof(DataServiceContext)), 
                        result.Arguments[0],
                        Expression.Constant(TrackingMode.AutoChangeTracking, typeof(TrackingMode)), 
                        Expression.Constant(null, typeof(string)), 
                        Expression.Constant(null, typeof(Func)),
                        Expression.Constant(null, typeof(Func))); 
                }
                else if (result.Arguments.Count == 2 &&
                         this.annotations.TryGetValue(result.Arguments[0], out annotation))
                { 
                    // DataServiceCollection(
                    //     IEnumerable items, 
                    //     TrackingMode trackingMode) 
                    // ->
                    // DataServiceCollection(materializer, null, items, trackingMode, null, null, null) 
                    result = Expression.New(
                        constructorInfo,
                        this.materializerExpression,
                        Expression.Constant(null, typeof(DataServiceContext)), 
                        result.Arguments[0], // items
                        result.Arguments[1], // TrackingMode 
                        Expression.Constant(null, typeof(string)), 
                        Expression.Constant(null, typeof(Func)),
                        Expression.Constant(null, typeof(Func))); 
                }
                else if (result.Arguments.Count == 5 &&
                         this.annotations.TryGetValue(result.Arguments[0], out annotation))
                { 
                    // DataServiceCollection(
                    //     IEnumerable items, 
                    //     TrackingMode trackingMode, 
                    //     string entitySet,
                    //     Func<> entityChangedCallback, 
                    //     Func<> entityCollectionChangedCallback)
                    // ->
                    // DataServiceCollection(materializer, null, items, trackingMode,
                    //     entitySet, entityChangedCallback, entityCollectionChangedCallback) 
                    result = Expression.New(
                        constructorInfo, 
                        this.materializerExpression, 
                        Expression.Constant(null, typeof(DataServiceContext)),
                        result.Arguments[0],   // items 
                        result.Arguments[1],   // TrackingMode
                        result.Arguments[2],   // entityset name
                        result.Arguments[3],   // entity changed cb
                        result.Arguments[4]);  // collection changed cb 
                }
                else if (result.Arguments.Count == 6 && 
                         typeof(DataServiceContext).IsAssignableFrom(result.Arguments[0].Type) && 
                         this.annotations.TryGetValue(result.Arguments[1], out annotation))
                { 
                    // DataServiceCollection(
                    //     DataServiceContext context,
                    //     IEnumerable items,
                    //     TrackingMode trackingMode, 
                    //     string entitySet,
                    //     Func<> entityChangedCallback, 
                    //     Func<> entityCollectionChangedCallback) 
                    // ->
                    // DataServiceCollection(materializer, context, items, trackingMode, 
                    //     entitySet, entityChangedCallback, entityCollectionChangedCallback)
                    result = Expression.New(
                        constructorInfo,
                        this.materializerExpression, 
                        result.Arguments[0],   // context
                        result.Arguments[1],   // items 
                        result.Arguments[2],   // trackingMode 
                        result.Arguments[3],   // entityset name
                        result.Arguments[4],   // entity changed cb 
                        result.Arguments[5]);  // collection changed cb
                }
            }
 
            if (annotation != null)
            { 
                // Propagate the annotation of the "items" parameter (the enumerable) as the annotation 
                //   of the DataServiceCollection, since it now represents the same thing.
                this.annotations.Add(result, annotation); 
            }

            // Note that we end up just falling through without changing anything for
            // constructors of DataServiceCollection that aren't correlated (i.e. don't take 
            // an enumerable in the input).
            return result; 
        } 

        /// Rewrites a call to Select() by adding to the current paths to project out. 
        /// Call expression.
        /// Expression with annotated path to include in member binding.
        private Expression RebindMethodCallForMemberSelect(MethodCallExpression call)
        { 
            Debug.Assert(call != null, "call != null");
            Debug.Assert(call.Method.Name == "Select", "call.Method.Name == 'Select'"); 
            Debug.Assert(call.Object == null, "call.Object == null -- otherwise this isn't a call to a static Select method"); 
            Debug.Assert(call.Arguments.Count == 2, "call.Arguments.Count == 2 -- otherwise this isn't the expected Select() call on IQueryable");
 
            // Get the path for the parameter value that will be used in the Select lambda.
            Expression result = null;
            Expression parameterSource = this.Visit(call.Arguments[0]);
            ExpressionAnnotation annotation; 
            this.annotations.TryGetValue(parameterSource, out annotation);
 
            // It's possibly that we haven't annotated this argument, in which 
            // case we don't care about this select, eg new { a = new int[] {1}.Select(i=>i+1).First() }
            if (annotation != null) 
            {
                // With this information from an annotation:
                // {t->*.Players}
                // 
                // Call this lambda:
                // {(mat, entry1, type1) => Convert(ProjectionValueForPath(mat, entry1, type1, p->*.FirstName))} 
                // 
                // Annotating the intermediate expressions so we mark them
                // as rewrites and we know to route them through materializer 
                // helpers, eg to preserve paging information.
                Expression selectorExpression = this.Visit(call.Arguments[1]);
                Type returnElementType = call.Method.ReturnType.GetGenericArguments()[0];
                result = CallMaterializer( 
                    "ProjectionSelect",
                    this.materializerExpression, 
                    this.pathBuilder.ParameterEntryInScope, 
                    this.pathBuilder.ExpectedParamTypeInScope,
                    Expression.Constant(returnElementType, typeof(Type)), 
                    Expression.Constant(annotation.Segment.StartPath, typeof(object)),
                    selectorExpression);
                this.annotations.Add(result, annotation);
                result = CallMaterializerWithType( 
                    "EnumerateAsElementType",
                    new Type[] { returnElementType }, 
                    result); 
                this.annotations.Add(result, annotation);
            } 

            if (result == null)
            {
                result = base.VisitMethodCall(call); 
            }
 
            return result; 
        }
 
        /// Rewrites a call to ToList in the specified method.
        /// Call expression.
        /// Expression with annotated path to include in member binding.
        ///  
        /// All that is necessary here is to rewrite the call to Select() and indicate
        /// that the target type is a given List<T>. 
        /// 
        ///
 
        private Expression RebindMethodCallForMemberToList(MethodCallExpression call)
        {
            Debug.Assert(call != null, "call != null");
            Debug.Assert(call.Object == null, "call.Object == null -- otherwise this isn't a call to a static ToList method"); 
            Debug.Assert(call.Method.Name == "ToList", "call.Method.Name == 'ToList'");
 
            // Wrap the source to the .ToList() call if it's our rewrite. 
            Debug.Assert(call.Arguments.Count == 1, "call.Arguments.Count == 1 -- otherwise this isn't the expected ToList() call on IEnumerable");
 
            Expression result = this.Visit(call.Arguments[0]);
            ExpressionAnnotation annotation;
            if (this.annotations.TryGetValue(result, out annotation))
            { 
                result = this.TypedEnumerableToList(result, call.Type);
                this.annotations.Add(result, annotation); 
            } 

            return result; 
        }

        /// Rewrites a method call used in a sequence method (possibly over entity types).
        /// Call expression. 
        /// Expression that can be called directly to yield the expected value.
        private Expression RebindMethodCallForNewSequence(MethodCallExpression call) 
        { 
            Debug.Assert(call != null, "call != null");
            Debug.Assert(ProjectionAnalyzer.IsMethodCallAllowedEntitySequence(call), "ProjectionAnalyzer.IsMethodCallAllowedEntitySequence(call)"); 
            Debug.Assert(call.Object == null, "call.Object == null -- otherwise this isn't the supported Select or ToList methods");

            // The only expressions that require rewriting are:
            // - [null].Select(entity-type-based-expression, lambda) 
            // - [null].ToList(entity-type-enumeration)
            // 
            // All other expressions can be visited normally. 
            Expression result = null;
 
            if (call.Method.Name == "Select")
            {
                Debug.Assert(call.Arguments.Count == 2, "call.Arguments.Count == 2 -- otherwise this isn't the argument we expected");
 
                // Get the path for the parameter value that will be used in the Select lambda.
                Expression parameterSource = this.Visit(call.Arguments[0]); 
                ExpressionAnnotation annotation; 
                this.annotations.TryGetValue(parameterSource, out annotation);
 
                // It's possibly that we haven't annotated this argument, in which
                // case we don't care about this select, eg new { a = new int[] {1}.Select(i=>i+1).First() }
                if (annotation != null)
                { 
                    // With this information from an annotation:
                    // {t->*.Players} 
                    // 
                    // Call this lambda:
                    // {(mat, entry1, type1) => Convert(ProjectionValueForPath(mat, entry1, type1, p->*.FirstName))} 
                    Expression selectorExpression = this.Visit(call.Arguments[1]);
                    Type returnElementType = call.Method.ReturnType.GetGenericArguments()[0];
                    result = CallMaterializer(
                        "ProjectionSelect", 
                        this.materializerExpression,
                        this.pathBuilder.ParameterEntryInScope, 
                        this.pathBuilder.ExpectedParamTypeInScope, 
                        Expression.Constant(returnElementType, typeof(Type)),
                        Expression.Constant(annotation.Segment.StartPath, typeof(object)), 
                        selectorExpression);
                    this.annotations.Add(result, annotation);
                    result = CallMaterializerWithType(
                        "EnumerateAsElementType", 
                        new Type[] { returnElementType },
                        result); 
                    this.annotations.Add(result, annotation); 
                }
            } 
            else
            {
                Debug.Assert(call.Method.Name == "ToList", "call.Method.Name == 'ToList'");
 
                // Annotate the source to the .ToList() call.
                Expression source = this.Visit(call.Arguments[0]); 
                ExpressionAnnotation annotation; 
                if (this.annotations.TryGetValue(source, out annotation))
                { 
                    result = this.TypedEnumerableToList(source, call.Type);
                    this.annotations.Add(result, annotation);
                }
            } 

            if (result == null) 
            { 
                result = base.VisitMethodCall(call);
            } 

            return result;
        }
 
        /// Returns a method call that returns a list from a typed enumerable.
        /// Expression to convert. 
        /// Target type to return. 
        /// The new expression.
        private Expression TypedEnumerableToList(Expression source, Type targetType) 
        {
            Debug.Assert(source != null, "source != null");
            Debug.Assert(targetType != null, "targetType != null");
 
            //
 
            Type enumeratedType = source.Type.GetGenericArguments()[0]; 
            Type listElementType = targetType.GetGenericArguments()[0];
 
            // Return the annotated expression.
            Expression result = CallMaterializerWithType(
                "ListAsElementType",
                new Type[] { enumeratedType, listElementType }, 
                this.materializerExpression,
                source); 
 
            return result;
        } 

        #endregion Private methods.

        #region Inner types. 

        /// Annotates an expression, typically from the target tree. 
        internal class ExpressionAnnotation 
        {
            /// Segment that marks the path found to an expression. 
            internal ProjectionPathSegment Segment
            {
                get;
                set; 
            }
        } 
 
        #endregion Inner types.
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK