/// <summary>
        /// If there is a MemberInitExpression 'new Person { ID = p.ID, Friend = new Person { ID = p.Friend.ID }}'
        /// or a NewExpression 'new { ID = p.ID, Friend = new { ID = p.Friend.ID }}',
        /// this method validates against the RHS of the member assignment, the expression "p.ID" for example.
        /// </summary>
        /// <param name="expressionToAssign">The expression to validate.</param>
        /// <param name="initType">Type of the MemberInit or the New expression.</param>
        /// <param name="previousNested">The outer nested initializer of the current initializer we are checking.</param>
        /// <returns>true if the expression to assign is fine; false otherwise.</returns>
        private bool CheckCompatibleAssignmentExpression(Expression expressionToAssign, Type initType, ref MemberAssignmentAnalysis previousNested)
        {
            MemberAssignmentAnalysis nested = MemberAssignmentAnalysis.Analyze(this.entity, expressionToAssign);

            if (nested.MultiplePathsFound)
            {
                this.multiplePathsFound = true;
                return(false);
            }

            // When we're visiting a nested entity initializer, we're exactly one level above that.
            Exception incompatibleException = nested.CheckCompatibleAssignments(initType, ref previousNested);

            if (incompatibleException != null)
            {
                this.incompatibleAssignmentsException = incompatibleException;
                return(false);
            }

            if (this.pathFromEntity.Count == 0)
            {
                this.pathFromEntity.AddRange(nested.GetExpressionsToTargetEntity());
            }

            return(true);
        }
        /// <summary>Analyzes an assignment from a member-init expression.</summary>
        /// <param name="entityInScope">Entity in scope for the lambda that's providing the parameter.</param>
        /// <param name="assignmentExpression">The expression to analyze.</param>
        /// <returns>The analysis results.</returns>
        internal static MemberAssignmentAnalysis Analyze(Expression entityInScope, Expression assignmentExpression)
        {
            Debug.Assert(entityInScope != null, "entityInScope != null");
            Debug.Assert(assignmentExpression != null, "assignmentExpression != null");

            MemberAssignmentAnalysis result = new MemberAssignmentAnalysis(entityInScope);

            result.Visit(assignmentExpression);
            return(result);
        }
        /// <summary>
        /// Checks whether the this and a <paramref name="previous"/>
        /// paths for assignments are compatible.
        /// </summary>
        /// <param name="targetType">Type being initialized.</param>
        /// <param name="previous">Previously seen member accesses (null if this is the first).</param>
        /// <returns>An exception to be thrown if assignments are not compatible; null otherwise.</returns>
        /// <remarks>
        /// This method does not set the IncompatibleAssignmentsException property on either
        /// analysis instance.
        /// </remarks>
        internal Exception CheckCompatibleAssignments(Type targetType, ref MemberAssignmentAnalysis previous)
        {
            if (previous == null)
            {
                previous = this;
                return(null);
            }

            Expression[] previousExpressions  = previous.GetExpressionsToTargetEntity();
            Expression[] candidateExpressions = this.GetExpressionsToTargetEntity();
            return(CheckCompatibleAssignments(targetType, previousExpressions, candidateExpressions));
        }
        /// <summary>Visits a nested member init.</summary>
        /// <param name="init">Expression to visit.</param>
        /// <returns>The same expression.</returns>
        internal override Expression VisitMemberInit(MemberInitExpression init)
        {
            Expression result = init;
            MemberAssignmentAnalysis previousNested = null;

            foreach (var binding in init.Bindings)
            {
                MemberAssignment assignment = binding as MemberAssignment;
                if (assignment == null)
                {
                    continue;
                }

                if (!this.CheckCompatibleAssignmentExpression(assignment.Expression, init.Type, ref previousNested))
                {
                    break;
                }
            }

            return(result);
        }
        /// <summary>
        /// NewExpression visit method
        /// </summary>
        /// <param name="nex">The NewExpression to visit</param>
        /// <returns>The visited NewExpression</returns>
        internal override NewExpression VisitNew(NewExpression nex)
        {
            if (nex.Members == null)
            {
                return(base.VisitNew(nex));
            }
            else
            {
                // Member init for an anonymous type.
                MemberAssignmentAnalysis previousNested = null;
                foreach (var arg in nex.Arguments)
                {
                    if (!this.CheckCompatibleAssignmentExpression(arg, nex.Type, ref previousNested))
                    {
                        break;
                    }
                }

                return(nex);
            }
        }
            /// <summary>Analyzes the specified member-init expression.</summary>
            /// <param name="mie">Expression to analyze.</param>
            /// <param name="pb">Path-tracking object to store analysis in.</param>
            /// <param name="context">Context of expression to analyze.</param>
            internal static void Analyze(MemberInitExpression mie, SelectExpandPathBuilder pb, DataServiceContext context)
            {
                Debug.Assert(mie != null, "mie != null");

                var epa = new EntityProjectionAnalyzer(pb, mie.Type, context);

                MemberAssignmentAnalysis targetEntityPath = null;

                foreach (MemberBinding mb in mie.Bindings)
                {
                    MemberAssignment ma = mb as MemberAssignment;
                    epa.Visit(ma.Expression);
                    if (ma != null)
                    {
                        var analysis = MemberAssignmentAnalysis.Analyze(pb.ParamExpressionInScope, ma.Expression);
                        if (analysis.IncompatibleAssignmentsException != null)
                        {
                            throw analysis.IncompatibleAssignmentsException;
                        }

                        // Note that an "empty" assignment on the binding is not checked/handled,
                        // because the funcletizer would have turned that into a constant
                        // in the tree, the visit earlier in this method would have thrown
                        // an exception at finding a constant in an entity projection.
                        //
                        // We do account however for failing to find a reference off the
                        // parameter entry to detect errors like this: new ET() { Ref = e }
                        // Here it looks like the new ET should be the parent of 'e', but
                        // there is nothing in scope that represents that.
                        //
                        // This also explains while error messages might be a bit misleading
                        // in this case (because they reference a constant when the user
                        // hasn't included any).
                        Type         targetType      = ClientTypeUtil.GetMemberType(ma.Member);
                        Expression[] lastExpressions = analysis.GetExpressionsBeyondTargetEntity();
                        if (lastExpressions.Length == 0)
                        {
                            throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjectionToEntity(targetType, ma.Expression));
                        }

                        MemberExpression lastExpression = lastExpressions[lastExpressions.Length - 1] as MemberExpression;
                        Debug.Assert(
                            !analysis.MultiplePathsFound,
                            "!analysis.MultiplePathsFound -- the initilizer has been visited, and cannot be empty, and expressions that can combine paths should have thrown exception during initializer analysis");
#if DEBUG
                        Debug.Assert(
                            lastExpression != null,
                            "lastExpression != null -- the initilizer has been visited, and cannot be empty, and the only expressions that are allowed can be formed off the parameter, so this is always correlatd");
#endif

                        analysis.CheckCompatibleAssignments(mie.Type, ref targetEntityPath);

                        // For DataServiceStreamLink, the last expression will be a constant expression. Hence we won't be comparing name checks and entity checks for those type of bindings
                        if (lastExpression != null)
                        {
                            if (lastExpression.Member.Name != ma.Member.Name)
                            {
                                throw new NotSupportedException(Strings.ALinq_PropertyNamesMustMatchInProjections(lastExpression.Member.Name, ma.Member.Name));
                            }

                            // Unless we're initializing an entity, we should not traverse into the parameter in scope.
                            bool targetIsEntity = ClientTypeUtil.TypeOrElementTypeIsEntity(targetType);
                            bool sourceIsEntity = ClientTypeUtil.TypeOrElementTypeIsEntity(lastExpression.Type);
                            if (sourceIsEntity && !targetIsEntity)
                            {
                                throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(targetType, ma.Expression));
                            }
                        }
                    }
                }
            }
        /// <summary>Analyzes an assignment from a member-init expression.</summary>
        /// <param name="entityInScope">Entity in scope for the lambda that's providing the parameter.</param>
        /// <param name="assignmentExpression">The expression to analyze.</param>
        /// <returns>The analysis results.</returns>
        internal static MemberAssignmentAnalysis Analyze(Expression entityInScope, Expression assignmentExpression)
        {
            Debug.Assert(entityInScope != null, "entityInScope != null");
            Debug.Assert(assignmentExpression != null, "assignmentExpression != null");

            MemberAssignmentAnalysis result = new MemberAssignmentAnalysis(entityInScope);
            result.Visit(assignmentExpression);
            return result;
        }
        /// <summary>
        /// If there is a MemberInitExpression 'new Person { ID = p.ID, Friend = new Person { ID = p.Friend.ID }}'
        /// or a NewExpression 'new { ID = p.ID, Friend = new { ID = p.Friend.ID }}',
        /// this method validates against the RHS of the member assigment, the expression "p.ID" for example.
        /// </summary>
        /// <param name="expressionToAssign">The expression to validate.</param>
        /// <param name="initType">Type of the MemberInit or the New expression.</param>
        /// <param name="previousNested">The outter nested initializer of the current initializer we are checking.</param>
        /// <returns>true if the expression to assign is fine; false otherwise.</returns>
        private bool CheckCompatibleAssigmentExpression(Expression expressionToAssign, Type initType, ref MemberAssignmentAnalysis previousNested)
        {
            MemberAssignmentAnalysis nested = MemberAssignmentAnalysis.Analyze(this.entity, expressionToAssign);
            if (nested.MultiplePathsFound)
            {
                this.multiplePathsFound = true;
                return false;
            }

            // When we're visitng a nested entity initializer, we're exactly one level above that.
            Exception incompatibleException = nested.CheckCompatibleAssignments(initType, ref previousNested);
            if (incompatibleException != null)
            {
                this.incompatibleAssignmentsException = incompatibleException;
                return false;
            }

            if (this.pathFromEntity.Count == 0)
            {
                this.pathFromEntity.AddRange(nested.GetExpressionsToTargetEntity());
            }

            return true;
        }
        /// <summary>
        /// Checks whether the this and a <paramref name="previous"/>
        /// paths for assignments are compatible.
        /// </summary>
        /// <param name="targetType">Type being initialized.</param>
        /// <param name="previous">Previously seen member accesses (null if this is the first).</param>
        /// <returns>An exception to be thrown if assignments are not compatible; null otherwise.</returns>
        /// <remarks>
        /// This method does not set the IncompatibleAssignmentsException property on either
        /// analysis instance.
        /// </remarks>
        internal Exception CheckCompatibleAssignments(Type targetType, ref MemberAssignmentAnalysis previous)
        {
            if (previous == null)
            {
                previous = this;
                return null;
            }

            Expression[] previousExpressions = previous.GetExpressionsToTargetEntity();
            Expression[] candidateExpressions = this.GetExpressionsToTargetEntity();
            return CheckCompatibleAssignments(targetType, previousExpressions, candidateExpressions);
        }