Example #1
0
        /// <summary>
        /// 根据指定表达式取表别名
        /// </summary>
        /// <param name="exp">表达式</param>
        /// <remarks>
        /// t=>t.Id
        /// t.Id
        /// </remarks>
        public string GetTableAlias(Expression exp)
        {
            // p=>p.p
            // p=>p.Id
            // p=>p.t.Id
            // p.Id
            // p.t.Id
            // p.t
            // <>h__TransparentIdentifier0.p.Id
            XFrameworkException.Check.NotNull(exp, "exp");
            string key = TableAliasCache.GetTableAliasKey(exp);

            return(this.GetTableAlias(key));
        }
Example #2
0
        private static string GetTableAliasKey(Expression exp)
        {
            if (exp == null)
            {
                return(null);
            }

            Expression expression = exp.ReduceUnary();

            //c
            if (exp.CanEvaluate())
            {
                return(null);
            }

            // p
            ParameterExpression paramExp = expression as ParameterExpression;

            if (paramExp != null)
            {
                return(paramExp.Name);
            }

            // a=>a.Id
            LambdaExpression lambdaExp = expression as LambdaExpression;

            if (lambdaExp != null)
            {
                expression = lambdaExp.Body.ReduceUnary();
            }

            // a.Id
            // t.a
            // t.t.a
            // t.a.Id
            MemberExpression memExp = expression as MemberExpression;

            if (memExp == null)
            {
                return(TableAliasCache.GetTableAliasKey(expression));
            }

            if (memExp.Acceptable())
            {
                return(TableAliasCache.GetTableAliasKey(memExp.Expression));
            }

            return(memExp.Member.Name);
        }