// Client = a.Client.CloudServer
        private Expression VisitNavigation(MemberExpression node, bool visitNavigation)
        {
            string alias = string.Empty;
            Type   type  = node.Type;

            if (node.Acceptable())
            {
                // 例: Client = a.Client.CloudServer
                // fix issue# Join 表达式显式指定导航属性时时,alias 为空
                // fix issue# 多个导航属性时 AppendNullColumn 只解析当前表达式的
                int index = 0;
                int num   = this.NavMembers != null ? this.NavMembers.Count : 0;
                alias = this.VisitNavMember(node);

                if (num != this.NavMembers.Count)
                {
                    foreach (var kvp in NavMembers)
                    {
                        index += 1;
                        if (index < NavMembers.Count && index > num)
                        {
                            alias = _aliases.GetNavigationTableAlias(kvp.Key);
                            //navKey = kvp.Key;
                            //if (visitNavigation) AppendNullColumn(kvp.Value.Member, alias, navKey);
                            continue;
                        }

                        //navKey = kvp.Key;
                        alias = _aliases.GetNavigationTableAlias(kvp.Key);
                        type  = kvp.Value.Type;
                    }
                }
                else
                {
                }
            }
            else
            {
                // 例: Client = b
                alias = _aliases.GetTableAlias(node);
                type  = node.Type;
            }

            if (type.IsGenericType)
            {
                type = type.GetGenericArguments()[0];
            }
            this.VisitAllMember(type, alias);
            if (visitNavigation)
            {
                AppendNullColumn(node.Member, alias);
            }

            return(node);
        }
Beispiel #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);
        }
        // => Client = a.Client.CloudServer
        private Expression VisitNavigation(MemberExpression node, bool visitNavigation, Expression pickExpression = null)
        {
            string alias = string.Empty;
            Type   type  = node.Type;

            if (node.Acceptable())
            {
                // 例: Client = a.Client.CloudServer
                // fix issue# Join 表达式显式指定导航属性时时,alias 为空
                // fix issue# 多个导航属性时 AppendNullColumn 只解析当前表达式的
                int index = 0;
                int num   = this.NavMembers != null ? this.NavMembers.Count : 0;
                alias = this.VisitNavMember(node);

                if (num != this.NavMembers.Count)
                {
                    foreach (var kvp in NavMembers)
                    {
                        index += 1;
                        if (index < NavMembers.Count && index > num)
                        {
                            alias = _aliases.GetNavigationTableAlias(kvp.Key);
                        }
                        else
                        {
                            alias = _aliases.GetNavigationTableAlias(kvp.Key);
                            type  = kvp.Value.Type;
                        }
                    }
                }
                else
                {
                }
            }
            else
            {
                // 例: Client = b
                alias = _aliases.GetTableAlias(node);
                type  = node.Type;
            }


            if (type.IsGenericType)
            {
                type = type.GetGenericArguments()[0];
            }
            if (pickExpression == null)
            {
                this.VisitAllMember(type, alias);
            }
            else
            {
                // Include 中选中的字段
                Expression expr = pickExpression;
                if (expr.NodeType == ExpressionType.Lambda)
                {
                    expr = (pickExpression as LambdaExpression).Body;
                }
                if (expr.NodeType == ExpressionType.New)
                {
                    var newExpression = expr as NewExpression;
                    for (int i = 0; i < newExpression.Arguments.Count; i++)
                    {
                        var memberExpression = newExpression.Arguments[i] as MemberExpression;
                        if (memberExpression == null)
                        {
                            throw new XFrameworkException("MemberExpression required at the {0} arguments.", i);
                        }

                        _builder.AppendMember(alias, memberExpression.Member.Name);
                        this.AddPickColumn(newExpression.Members != null ? newExpression.Members[i].Name : memberExpression.Member.Name);
                    }
                }
                else if (expr.NodeType == ExpressionType.MemberInit)
                {
                    var initExpression = expr as MemberInitExpression;
                    for (int i = 0; i < initExpression.Bindings.Count; i++)
                    {
                        var binding = initExpression.Bindings[i] as MemberAssignment;
                        if (binding == null)
                        {
                            throw new XFrameworkException("Only 'MemberAssignment' binding supported.");
                        }

                        var memberExpression = binding.Expression as MemberExpression;
                        if (memberExpression == null)
                        {
                            throw new XFrameworkException("MemberExpression required at the {0} arguments.", i);
                        }

                        _builder.AppendMember(alias, memberExpression.Member.Name);
                        this.AddPickColumn(binding.Member.Name);
                    }
                }
                else
                {
                    throw new NotSupportedException(string.Format("Include method not supporte {0}", expr.NodeType));
                }
            }

            if (visitNavigation)
            {
                AddSplitOnColumn(node.Member, alias);
            }

            return(node);
        }