ExpressionReplacer.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / ndp / fx / src / DataEntity / System / Data / Common / CommandTrees / Internal / ExpressionReplacer.cs / 2 / ExpressionReplacer.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner  [....], [....]
//--------------------------------------------------------------------- 
 
using System;
using System.Collections.Generic; 
using System.Diagnostics;
using System.Data.Metadata.Edm;
using System.Data.Common.CommandTrees;
 
namespace System.Data.Common.CommandTrees.Internal
{ 
    internal class ExpressionReplacement 
    {
        private DbExpression _toReplace; 
        private DbExpression _replacement;
        private bool _visit;

        internal ExpressionReplacement() { } 

        public DbExpression Current { get { return _toReplace; } internal set { _toReplace = value; } } 
        public DbExpression Replacement 
        {
            get { return _replacement; } 
            set
            {
                // #487325
                if (value == null) 
                {
                    throw EntityUtil.ArgumentNull("Replacement"); 
                } 
                _replacement = value;
            } 
        }
        public bool VisitReplacement { get { return _visit; } set { _visit = value; } }
    }
 
    internal delegate void ReplacerCallback(ExpressionReplacement replacement);
 
    ///  
    /// Replaces Expressions according to a given callback
    ///  
    internal class ExpressionReplacer : DbExpressionVisitor
    {
        private ReplacerCallback _replacer;
        private ExpressionReplacement _replacement = new ExpressionReplacement(); 

        internal ExpressionReplacer(ReplacerCallback replacer) 
        { 
            _replacer = replacer;
        } 

        internal DbExpression Replace(DbExpression toReplace)
        {
            return toReplace.Accept(this); 
        }
 
        internal void ReplaceBinding(DbExpressionBinding binding) 
        {
            DbExpression newExpr = this.Replace(binding.Expression); 
            if (!object.ReferenceEquals(newExpr, binding.Expression))
            {
                binding.Expression = newExpr;
            } 
        }
 
        #region Private Implementation 

        ///  
        /// Handles the logic of invoking the replacement callback, inspecting the result, and recursing into the new expression if necessary.
        /// This method is guaranteed to return either the original expression or the replaced expression as the value of the out parameter
        /// (i.e. will never return null) with the Boolean return value indicating whether or not replacement actually took place.
        ///  
        /// The DbExpression to potentially replace
        /// Either the original DbExpression or the DbExpression that the replacement callback replaced the original DbExpression with 
        /// True if the original DbExpression was replaced and the result is the new DbExpression; false otherwise (in which case result is the original DbExpression) 
        private bool DoReplacement(DbExpression original, out DbExpression result)
        { 
            result = original;

            _replacement.Current = original;
            _replacement.Replacement = original; 
            _replacement.VisitReplacement = true;
 
            _replacer(_replacement); 

            if (!object.ReferenceEquals(_replacement.Current, _replacement.Replacement)) 
            {
                if (_replacement.VisitReplacement)
                {
                    result = this.Replace(_replacement.Replacement); 
                }
                else 
                { 
                    result = _replacement.Replacement;
                } 

                return true;
            }
 
            return false;
        } 
 
        private DbExpression ReplaceTerminal(DbExpression e)
        { 
            DbExpression retExpr = null;
            DoReplacement(e, out retExpr);
            return retExpr;
        } 

        private DbExpression ReplaceUnary(DbUnaryExpression e) 
        { 
            DbExpression retExpr = null;
            if (DoReplacement(e, out retExpr)) 
            {
                return retExpr;
            }
 
            DbExpression argExpr = this.Replace(e.Argument);
            if (!object.ReferenceEquals(argExpr, e.Argument)) 
            { 
                e.Argument = argExpr;
            } 

            return e;
        }
 
        private DbExpression ReplaceBinary(DbBinaryExpression e)
        { 
            DbExpression retExpr = null; 
            if (DoReplacement(e, out retExpr))
            { 
                return retExpr;
            }

            DbExpression argExpr = this.Replace(e.Left); 
            if (!object.ReferenceEquals(argExpr, e.Left))
            { 
                e.Left = argExpr; 
            }
 
            argExpr = this.Replace(e.Right);
            if (!object.ReferenceEquals(argExpr, e.Right))
            {
                e.Right = argExpr; 
            }
 
            return e; 
        }
 
        private void ReplaceList(IList list)
        {
            for (int idx = 0; idx < list.Count; idx++)
            { 
                DbExpression newExpr = this.Replace(list[idx]);
                if (!object.ReferenceEquals(newExpr, list[idx])) 
                { 
#if MUTABLE_CQT
                    list[idx] = newExpr; 
#else
                    ExpressionList exprList = list as ExpressionList;
                    if (exprList != null)
                    { 
                        exprList.SetAt(idx, newExpr);
                    } 
                    else 
                    {
                        list[idx] = newExpr; 
                    }
#endif
                }
            } 

        } 
 
        private void ReplaceBinding(DbGroupExpressionBinding binding)
        { 
            DbExpression newExpr = this.Replace(binding.Expression);
            if (!object.ReferenceEquals(newExpr, binding.Expression))
            {
                binding.Expression = newExpr; 
            }
        } 
 
        #endregion
 
        #region DbExpressionVisitor Members

        public override DbExpression Visit(DbExpression e)
        { 
            throw EntityUtil.NotSupported(System.Data.Entity.Strings.Cqt_General_UnsupportedExpression(e.GetType().FullName));
        } 
 
        public override DbExpression Visit(DbConstantExpression e)
        { 
            return ReplaceTerminal(e);
        }

        public override DbExpression Visit(DbNullExpression e) 
        {
            return ReplaceTerminal(e); 
        } 

        public override DbExpression Visit(DbVariableReferenceExpression e) 
        {
            return ReplaceTerminal(e);
        }
 
        public override DbExpression Visit(DbParameterReferenceExpression e)
        { 
            return ReplaceTerminal(e); 
        }
 
        public override DbExpression Visit(DbFunctionExpression e)
        {
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr)) 
            {
                return newExpr; 
            } 

            ReplaceList(e.Arguments); 

            if (e.IsLambda)
            {
                Debug.Assert(e.LambdaBody != null, "DbFunctionExpression.IsLambda returned true when LambdaBody == null"); 
                DbExpression newBody = this.Replace(e.LambdaBody);
                if (!object.ReferenceEquals(newBody, e.LambdaBody)) 
                { 
                    e.LambdaBody = newBody;
                } 
            }

            return e;
        } 

#if METHOD_EXPRESSION 
        public override Expression Visit(MethodExpression e) 
        {
            Expression newExpr = null; 
            if (DoReplacement(e, out newExpr))
            {
                return newExpr;
            } 

            ReplaceList(e.Arguments); 
 
            if (e.Instance != null)
            { 
                Expression newInst = this.Replace(e.Instance);
                if (!object.ReferenceEquals(newInst, e.Instance))
                {
                    e.Instance = newInst; 
                }
            } 
 
            return e;
        } 
#endif

        public override DbExpression Visit(DbPropertyExpression e)
        { 
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr)) 
            { 
                return newExpr;
            } 

            if (e.Instance != null)
            {
                DbExpression newInst = this.Replace(e.Instance); 
                if (!object.ReferenceEquals(newInst, e.Instance))
                { 
                    e.Instance = newInst; 
                }
            } 

            return e;
        }
 
        public override DbExpression Visit(DbComparisonExpression e)
        { 
            return ReplaceBinary(e); 
        }
 
        public override DbExpression Visit(DbLikeExpression e)
        {
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr)) 
            {
                return newExpr; 
            } 

            DbExpression argExpr = this.Replace(e.Argument); 
            if (!object.ReferenceEquals(argExpr, e.Argument))
            {
                e.Argument = argExpr;
            } 

            argExpr = this.Replace(e.Pattern); 
            if (!object.ReferenceEquals(argExpr, e.Pattern)) 
            {
                e.Pattern = argExpr; 
            }

            argExpr = this.Replace(e.Escape);
            if (!object.ReferenceEquals(argExpr, e.Escape)) 
            {
                e.Escape = argExpr; 
            } 

            return e; 
        }

        public override DbExpression Visit(DbLimitExpression e)
        { 
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr)) 
            { 
                return newExpr;
            } 

            DbExpression argExpr = this.Replace(e.Argument);
            if (!object.ReferenceEquals(argExpr, e.Argument))
            { 
                e.Argument = argExpr;
            } 
 
            argExpr = this.Replace(e.Limit);
            if (!object.ReferenceEquals(argExpr, e.Limit)) 
            {
                e.Limit = argExpr;
            }
 
            return e;
        } 
 
        public override DbExpression Visit(DbIsNullExpression e)
        { 
            return ReplaceUnary(e);
        }

        public override DbExpression Visit(DbArithmeticExpression e) 
        {
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr)) 
            {
                return newExpr; 
            }

            ReplaceList(e.Arguments);
 
            return e;
        } 
 
        public override DbExpression Visit(DbAndExpression e)
        { 
            return ReplaceBinary(e);
        }

        public override DbExpression Visit(DbOrExpression e) 
        {
            return ReplaceBinary(e); 
        } 

        public override DbExpression Visit(DbNotExpression e) 
        {
            return ReplaceUnary(e);
        }
 
        public override DbExpression Visit(DbDistinctExpression e)
        { 
            return ReplaceUnary(e); 
        }
 
        public override DbExpression Visit(DbElementExpression e)
        {
            return ReplaceUnary(e);
        } 

        public override DbExpression Visit(DbIsEmptyExpression e) 
        { 
            return ReplaceUnary(e);
        } 

        public override DbExpression Visit(DbUnionAllExpression e)
        {
            return ReplaceBinary(e); 
        }
 
        public override DbExpression Visit(DbIntersectExpression e) 
        {
            return ReplaceBinary(e); 
        }

        public override DbExpression Visit(DbExceptExpression e)
        { 
            return ReplaceBinary(e);
        } 
 
        public override DbExpression Visit(DbTreatExpression e)
        { 
            return ReplaceUnary(e);
        }

        public override DbExpression Visit(DbIsOfExpression e) 
        {
            return ReplaceUnary(e); 
        } 

        public override DbExpression Visit(DbCastExpression e) 
        {
            return ReplaceUnary(e);
        }
 
        public override DbExpression Visit(DbCaseExpression e)
        { 
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr))
            { 
                return newExpr;
            }

            ReplaceList(e.When); 
            ReplaceList(e.Then);
            newExpr = this.Replace(e.Else); 
            if (!object.ReferenceEquals(newExpr, e.Else)) 
            {
                e.Else = newExpr; 
            }

            return e;
        } 

        public override DbExpression Visit(DbOfTypeExpression e) 
        { 
            return ReplaceUnary(e);
        } 

        public override DbExpression Visit(DbNewInstanceExpression e)
        {
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr))
            { 
                return newExpr; 
            }
 
            ReplaceList(e.Arguments);
            if (e.HasRelatedEntityReferences)
            {
                foreach(DbRelatedEntityRef relatedRef in e.RelatedEntityReferences) 
                {
                    DbExpression newTargetRef = this.Replace(relatedRef.TargetEntityReference); 
                    if (!object.ReferenceEquals(newTargetRef, relatedRef.TargetEntityReference)) 
                    {
                        relatedRef.TargetEntityReference = newTargetRef; 
                    }
                }
            }
 
            return e;
        } 
 
        public override DbExpression Visit(DbRefExpression e)
        { 
            return ReplaceUnary(e);
        }

        public override DbExpression Visit(DbRelationshipNavigationExpression e) 
        {
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr)) 
            {
                return newExpr; 
            }

            DbExpression argExpr = this.Replace(e.NavigationSource);
            if (!object.ReferenceEquals(argExpr, e.NavigationSource)) 
            {
                e.NavigationSource = argExpr; 
            } 

            return e; 
        }

        public override DbExpression Visit(DbDerefExpression e)
        { 
            return ReplaceUnary(e);
        } 
 
        public override DbExpression Visit(DbRefKeyExpression e)
        { 
            return ReplaceUnary(e);
        }

        public override DbExpression Visit(DbEntityRefExpression e) 
        {
            return ReplaceUnary(e); 
        } 

        public override DbExpression Visit(DbScanExpression e) 
        {
            return ReplaceTerminal(e);
        }
 
        public override DbExpression Visit(DbFilterExpression e)
        { 
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr))
            { 
                return newExpr;
            }

            ReplaceBinding(e.Input); 

            DbExpression predExpr = this.Replace(e.Predicate); 
            if (!object.ReferenceEquals(predExpr, e.Predicate)) 
            {
                e.Predicate = predExpr; 
            }

            return e;
        } 

        public override DbExpression Visit(DbProjectExpression e) 
        { 
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr)) 
            {
                return newExpr;
            }
 
            ReplaceBinding(e.Input);
 
            DbExpression projExpr = this.Replace(e.Projection); 
            if (!object.ReferenceEquals(projExpr, e.Projection))
            { 
                e.Projection = projExpr;
            }

            return e; 
        }
 
        public override DbExpression Visit(DbCrossJoinExpression e) 
        {
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr))
            {
                return newExpr;
            } 

            foreach (DbExpressionBinding input in e.Inputs) 
            { 
                ReplaceBinding(input);
            } 

            return e;
        }
 
        public override DbExpression Visit(DbJoinExpression e)
        { 
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr))
            { 
                return newExpr;
            }

            ReplaceBinding(e.Left); 
            ReplaceBinding(e.Right);
 
            DbExpression condExpr = this.Replace(e.JoinCondition); 
            if (!object.ReferenceEquals(condExpr, e.JoinCondition))
            { 
                e.JoinCondition = condExpr;
            }

            return e; 
        }
 
        public override DbExpression Visit(DbApplyExpression e) 
        {
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr))
            {
                return newExpr;
            } 

            ReplaceBinding(e.Input); 
            ReplaceBinding(e.Apply); 

            return e; 
        }

        public override DbExpression Visit(DbGroupByExpression e)
        { 
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr)) 
            { 
                return newExpr;
            } 

            ReplaceBinding(e.Input);
            ReplaceList(e.Keys);
            foreach (DbAggregate agg in e.Aggregates) 
            {
                ReplaceList(agg.Arguments); 
            } 

            return e; 
        }

        private void ReplaceSortKeys(IList keys)
        { 
            foreach (DbSortClause sortClause in keys)
            { 
                DbExpression keyExpr = this.Replace(sortClause.Expression); 
                if (!object.ReferenceEquals(keyExpr, sortClause.Expression))
                { 
                    sortClause.Expression = keyExpr;
                }
            }
        } 

        public override DbExpression Visit(DbSkipExpression e) 
        { 
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr)) 
            {
                return newExpr;
            }
 
            ReplaceBinding(e.Input);
            ReplaceSortKeys(e.SortOrder); 
 
            DbExpression countExpr = this.Replace(e.Count);
            if (!object.ReferenceEquals(countExpr, e.Count)) 
            {
                e.Count = countExpr;
            }
 
            return e;
        } 
 
        public override DbExpression Visit(DbSortExpression e)
        { 
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr))
            {
                return newExpr; 
            }
 
            ReplaceBinding(e.Input); 
            ReplaceSortKeys(e.SortOrder);
 
            return e;
        }

        public override DbExpression Visit(DbQuantifierExpression e) 
        {
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr)) 
            {
                return newExpr; 
            }

            ReplaceBinding(e.Input);
 
            DbExpression predExpr = this.Replace(e.Predicate);
            if (!object.ReferenceEquals(predExpr, e.Predicate)) 
            { 
                e.Predicate = predExpr;
            } 

            return e;
        }
 
        #endregion
    } 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner  [....], [....]
//--------------------------------------------------------------------- 
 
using System;
using System.Collections.Generic; 
using System.Diagnostics;
using System.Data.Metadata.Edm;
using System.Data.Common.CommandTrees;
 
namespace System.Data.Common.CommandTrees.Internal
{ 
    internal class ExpressionReplacement 
    {
        private DbExpression _toReplace; 
        private DbExpression _replacement;
        private bool _visit;

        internal ExpressionReplacement() { } 

        public DbExpression Current { get { return _toReplace; } internal set { _toReplace = value; } } 
        public DbExpression Replacement 
        {
            get { return _replacement; } 
            set
            {
                // #487325
                if (value == null) 
                {
                    throw EntityUtil.ArgumentNull("Replacement"); 
                } 
                _replacement = value;
            } 
        }
        public bool VisitReplacement { get { return _visit; } set { _visit = value; } }
    }
 
    internal delegate void ReplacerCallback(ExpressionReplacement replacement);
 
    ///  
    /// Replaces Expressions according to a given callback
    ///  
    internal class ExpressionReplacer : DbExpressionVisitor
    {
        private ReplacerCallback _replacer;
        private ExpressionReplacement _replacement = new ExpressionReplacement(); 

        internal ExpressionReplacer(ReplacerCallback replacer) 
        { 
            _replacer = replacer;
        } 

        internal DbExpression Replace(DbExpression toReplace)
        {
            return toReplace.Accept(this); 
        }
 
        internal void ReplaceBinding(DbExpressionBinding binding) 
        {
            DbExpression newExpr = this.Replace(binding.Expression); 
            if (!object.ReferenceEquals(newExpr, binding.Expression))
            {
                binding.Expression = newExpr;
            } 
        }
 
        #region Private Implementation 

        ///  
        /// Handles the logic of invoking the replacement callback, inspecting the result, and recursing into the new expression if necessary.
        /// This method is guaranteed to return either the original expression or the replaced expression as the value of the out parameter
        /// (i.e. will never return null) with the Boolean return value indicating whether or not replacement actually took place.
        ///  
        /// The DbExpression to potentially replace
        /// Either the original DbExpression or the DbExpression that the replacement callback replaced the original DbExpression with 
        /// True if the original DbExpression was replaced and the result is the new DbExpression; false otherwise (in which case result is the original DbExpression) 
        private bool DoReplacement(DbExpression original, out DbExpression result)
        { 
            result = original;

            _replacement.Current = original;
            _replacement.Replacement = original; 
            _replacement.VisitReplacement = true;
 
            _replacer(_replacement); 

            if (!object.ReferenceEquals(_replacement.Current, _replacement.Replacement)) 
            {
                if (_replacement.VisitReplacement)
                {
                    result = this.Replace(_replacement.Replacement); 
                }
                else 
                { 
                    result = _replacement.Replacement;
                } 

                return true;
            }
 
            return false;
        } 
 
        private DbExpression ReplaceTerminal(DbExpression e)
        { 
            DbExpression retExpr = null;
            DoReplacement(e, out retExpr);
            return retExpr;
        } 

        private DbExpression ReplaceUnary(DbUnaryExpression e) 
        { 
            DbExpression retExpr = null;
            if (DoReplacement(e, out retExpr)) 
            {
                return retExpr;
            }
 
            DbExpression argExpr = this.Replace(e.Argument);
            if (!object.ReferenceEquals(argExpr, e.Argument)) 
            { 
                e.Argument = argExpr;
            } 

            return e;
        }
 
        private DbExpression ReplaceBinary(DbBinaryExpression e)
        { 
            DbExpression retExpr = null; 
            if (DoReplacement(e, out retExpr))
            { 
                return retExpr;
            }

            DbExpression argExpr = this.Replace(e.Left); 
            if (!object.ReferenceEquals(argExpr, e.Left))
            { 
                e.Left = argExpr; 
            }
 
            argExpr = this.Replace(e.Right);
            if (!object.ReferenceEquals(argExpr, e.Right))
            {
                e.Right = argExpr; 
            }
 
            return e; 
        }
 
        private void ReplaceList(IList list)
        {
            for (int idx = 0; idx < list.Count; idx++)
            { 
                DbExpression newExpr = this.Replace(list[idx]);
                if (!object.ReferenceEquals(newExpr, list[idx])) 
                { 
#if MUTABLE_CQT
                    list[idx] = newExpr; 
#else
                    ExpressionList exprList = list as ExpressionList;
                    if (exprList != null)
                    { 
                        exprList.SetAt(idx, newExpr);
                    } 
                    else 
                    {
                        list[idx] = newExpr; 
                    }
#endif
                }
            } 

        } 
 
        private void ReplaceBinding(DbGroupExpressionBinding binding)
        { 
            DbExpression newExpr = this.Replace(binding.Expression);
            if (!object.ReferenceEquals(newExpr, binding.Expression))
            {
                binding.Expression = newExpr; 
            }
        } 
 
        #endregion
 
        #region DbExpressionVisitor Members

        public override DbExpression Visit(DbExpression e)
        { 
            throw EntityUtil.NotSupported(System.Data.Entity.Strings.Cqt_General_UnsupportedExpression(e.GetType().FullName));
        } 
 
        public override DbExpression Visit(DbConstantExpression e)
        { 
            return ReplaceTerminal(e);
        }

        public override DbExpression Visit(DbNullExpression e) 
        {
            return ReplaceTerminal(e); 
        } 

        public override DbExpression Visit(DbVariableReferenceExpression e) 
        {
            return ReplaceTerminal(e);
        }
 
        public override DbExpression Visit(DbParameterReferenceExpression e)
        { 
            return ReplaceTerminal(e); 
        }
 
        public override DbExpression Visit(DbFunctionExpression e)
        {
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr)) 
            {
                return newExpr; 
            } 

            ReplaceList(e.Arguments); 

            if (e.IsLambda)
            {
                Debug.Assert(e.LambdaBody != null, "DbFunctionExpression.IsLambda returned true when LambdaBody == null"); 
                DbExpression newBody = this.Replace(e.LambdaBody);
                if (!object.ReferenceEquals(newBody, e.LambdaBody)) 
                { 
                    e.LambdaBody = newBody;
                } 
            }

            return e;
        } 

#if METHOD_EXPRESSION 
        public override Expression Visit(MethodExpression e) 
        {
            Expression newExpr = null; 
            if (DoReplacement(e, out newExpr))
            {
                return newExpr;
            } 

            ReplaceList(e.Arguments); 
 
            if (e.Instance != null)
            { 
                Expression newInst = this.Replace(e.Instance);
                if (!object.ReferenceEquals(newInst, e.Instance))
                {
                    e.Instance = newInst; 
                }
            } 
 
            return e;
        } 
#endif

        public override DbExpression Visit(DbPropertyExpression e)
        { 
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr)) 
            { 
                return newExpr;
            } 

            if (e.Instance != null)
            {
                DbExpression newInst = this.Replace(e.Instance); 
                if (!object.ReferenceEquals(newInst, e.Instance))
                { 
                    e.Instance = newInst; 
                }
            } 

            return e;
        }
 
        public override DbExpression Visit(DbComparisonExpression e)
        { 
            return ReplaceBinary(e); 
        }
 
        public override DbExpression Visit(DbLikeExpression e)
        {
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr)) 
            {
                return newExpr; 
            } 

            DbExpression argExpr = this.Replace(e.Argument); 
            if (!object.ReferenceEquals(argExpr, e.Argument))
            {
                e.Argument = argExpr;
            } 

            argExpr = this.Replace(e.Pattern); 
            if (!object.ReferenceEquals(argExpr, e.Pattern)) 
            {
                e.Pattern = argExpr; 
            }

            argExpr = this.Replace(e.Escape);
            if (!object.ReferenceEquals(argExpr, e.Escape)) 
            {
                e.Escape = argExpr; 
            } 

            return e; 
        }

        public override DbExpression Visit(DbLimitExpression e)
        { 
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr)) 
            { 
                return newExpr;
            } 

            DbExpression argExpr = this.Replace(e.Argument);
            if (!object.ReferenceEquals(argExpr, e.Argument))
            { 
                e.Argument = argExpr;
            } 
 
            argExpr = this.Replace(e.Limit);
            if (!object.ReferenceEquals(argExpr, e.Limit)) 
            {
                e.Limit = argExpr;
            }
 
            return e;
        } 
 
        public override DbExpression Visit(DbIsNullExpression e)
        { 
            return ReplaceUnary(e);
        }

        public override DbExpression Visit(DbArithmeticExpression e) 
        {
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr)) 
            {
                return newExpr; 
            }

            ReplaceList(e.Arguments);
 
            return e;
        } 
 
        public override DbExpression Visit(DbAndExpression e)
        { 
            return ReplaceBinary(e);
        }

        public override DbExpression Visit(DbOrExpression e) 
        {
            return ReplaceBinary(e); 
        } 

        public override DbExpression Visit(DbNotExpression e) 
        {
            return ReplaceUnary(e);
        }
 
        public override DbExpression Visit(DbDistinctExpression e)
        { 
            return ReplaceUnary(e); 
        }
 
        public override DbExpression Visit(DbElementExpression e)
        {
            return ReplaceUnary(e);
        } 

        public override DbExpression Visit(DbIsEmptyExpression e) 
        { 
            return ReplaceUnary(e);
        } 

        public override DbExpression Visit(DbUnionAllExpression e)
        {
            return ReplaceBinary(e); 
        }
 
        public override DbExpression Visit(DbIntersectExpression e) 
        {
            return ReplaceBinary(e); 
        }

        public override DbExpression Visit(DbExceptExpression e)
        { 
            return ReplaceBinary(e);
        } 
 
        public override DbExpression Visit(DbTreatExpression e)
        { 
            return ReplaceUnary(e);
        }

        public override DbExpression Visit(DbIsOfExpression e) 
        {
            return ReplaceUnary(e); 
        } 

        public override DbExpression Visit(DbCastExpression e) 
        {
            return ReplaceUnary(e);
        }
 
        public override DbExpression Visit(DbCaseExpression e)
        { 
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr))
            { 
                return newExpr;
            }

            ReplaceList(e.When); 
            ReplaceList(e.Then);
            newExpr = this.Replace(e.Else); 
            if (!object.ReferenceEquals(newExpr, e.Else)) 
            {
                e.Else = newExpr; 
            }

            return e;
        } 

        public override DbExpression Visit(DbOfTypeExpression e) 
        { 
            return ReplaceUnary(e);
        } 

        public override DbExpression Visit(DbNewInstanceExpression e)
        {
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr))
            { 
                return newExpr; 
            }
 
            ReplaceList(e.Arguments);
            if (e.HasRelatedEntityReferences)
            {
                foreach(DbRelatedEntityRef relatedRef in e.RelatedEntityReferences) 
                {
                    DbExpression newTargetRef = this.Replace(relatedRef.TargetEntityReference); 
                    if (!object.ReferenceEquals(newTargetRef, relatedRef.TargetEntityReference)) 
                    {
                        relatedRef.TargetEntityReference = newTargetRef; 
                    }
                }
            }
 
            return e;
        } 
 
        public override DbExpression Visit(DbRefExpression e)
        { 
            return ReplaceUnary(e);
        }

        public override DbExpression Visit(DbRelationshipNavigationExpression e) 
        {
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr)) 
            {
                return newExpr; 
            }

            DbExpression argExpr = this.Replace(e.NavigationSource);
            if (!object.ReferenceEquals(argExpr, e.NavigationSource)) 
            {
                e.NavigationSource = argExpr; 
            } 

            return e; 
        }

        public override DbExpression Visit(DbDerefExpression e)
        { 
            return ReplaceUnary(e);
        } 
 
        public override DbExpression Visit(DbRefKeyExpression e)
        { 
            return ReplaceUnary(e);
        }

        public override DbExpression Visit(DbEntityRefExpression e) 
        {
            return ReplaceUnary(e); 
        } 

        public override DbExpression Visit(DbScanExpression e) 
        {
            return ReplaceTerminal(e);
        }
 
        public override DbExpression Visit(DbFilterExpression e)
        { 
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr))
            { 
                return newExpr;
            }

            ReplaceBinding(e.Input); 

            DbExpression predExpr = this.Replace(e.Predicate); 
            if (!object.ReferenceEquals(predExpr, e.Predicate)) 
            {
                e.Predicate = predExpr; 
            }

            return e;
        } 

        public override DbExpression Visit(DbProjectExpression e) 
        { 
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr)) 
            {
                return newExpr;
            }
 
            ReplaceBinding(e.Input);
 
            DbExpression projExpr = this.Replace(e.Projection); 
            if (!object.ReferenceEquals(projExpr, e.Projection))
            { 
                e.Projection = projExpr;
            }

            return e; 
        }
 
        public override DbExpression Visit(DbCrossJoinExpression e) 
        {
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr))
            {
                return newExpr;
            } 

            foreach (DbExpressionBinding input in e.Inputs) 
            { 
                ReplaceBinding(input);
            } 

            return e;
        }
 
        public override DbExpression Visit(DbJoinExpression e)
        { 
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr))
            { 
                return newExpr;
            }

            ReplaceBinding(e.Left); 
            ReplaceBinding(e.Right);
 
            DbExpression condExpr = this.Replace(e.JoinCondition); 
            if (!object.ReferenceEquals(condExpr, e.JoinCondition))
            { 
                e.JoinCondition = condExpr;
            }

            return e; 
        }
 
        public override DbExpression Visit(DbApplyExpression e) 
        {
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr))
            {
                return newExpr;
            } 

            ReplaceBinding(e.Input); 
            ReplaceBinding(e.Apply); 

            return e; 
        }

        public override DbExpression Visit(DbGroupByExpression e)
        { 
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr)) 
            { 
                return newExpr;
            } 

            ReplaceBinding(e.Input);
            ReplaceList(e.Keys);
            foreach (DbAggregate agg in e.Aggregates) 
            {
                ReplaceList(agg.Arguments); 
            } 

            return e; 
        }

        private void ReplaceSortKeys(IList keys)
        { 
            foreach (DbSortClause sortClause in keys)
            { 
                DbExpression keyExpr = this.Replace(sortClause.Expression); 
                if (!object.ReferenceEquals(keyExpr, sortClause.Expression))
                { 
                    sortClause.Expression = keyExpr;
                }
            }
        } 

        public override DbExpression Visit(DbSkipExpression e) 
        { 
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr)) 
            {
                return newExpr;
            }
 
            ReplaceBinding(e.Input);
            ReplaceSortKeys(e.SortOrder); 
 
            DbExpression countExpr = this.Replace(e.Count);
            if (!object.ReferenceEquals(countExpr, e.Count)) 
            {
                e.Count = countExpr;
            }
 
            return e;
        } 
 
        public override DbExpression Visit(DbSortExpression e)
        { 
            DbExpression newExpr = null;
            if (DoReplacement(e, out newExpr))
            {
                return newExpr; 
            }
 
            ReplaceBinding(e.Input); 
            ReplaceSortKeys(e.SortOrder);
 
            return e;
        }

        public override DbExpression Visit(DbQuantifierExpression e) 
        {
            DbExpression newExpr = null; 
            if (DoReplacement(e, out newExpr)) 
            {
                return newExpr; 
            }

            ReplaceBinding(e.Input);
 
            DbExpression predExpr = this.Replace(e.Predicate);
            if (!object.ReferenceEquals(predExpr, e.Predicate)) 
            { 
                e.Predicate = predExpr;
            } 

            return e;
        }
 
        #endregion
    } 
} 

// 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