// 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
        // 访问导航属性
        protected virtual string VisitNavMember(Expression expression, string memberName = null)
        {
            // 表达式 => b.Client.Address.AddressName
            Expression node = expression;
            Stack <KeyValuePair <string, MemberExpression> > stack = null;
            string alias = string.Empty;

            while (node != null && node.Acceptable())
            {
                if (node.NodeType != ExpressionType.MemberAccess)
                {
                    break;
                }

                if (stack == null)
                {
                    stack = new Stack <KeyValuePair <string, MemberExpression> >();
                }
                MemberExpression memberExpression = node as MemberExpression;

                TypeRuntimeInfo     typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(memberExpression.Expression.Type);
                ForeignKeyAttribute attribute   = typeRuntime.GetInvokerAttribute <ForeignKeyAttribute>(memberExpression.Member.Name);
                if (attribute == null)
                {
                    break;
                }

                string key = memberExpression.GetKeyWidthoutAnonymous();
                stack.Push(new KeyValuePair <string, MemberExpression>(key, memberExpression));
                node = memberExpression.Expression;
                if (node.NodeType == ExpressionType.Call)
                {
                    node = (node as MethodCallExpression).Object;
                }
            }

            if (stack != null && stack.Count > 0)
            {
                while (stack != null && stack.Count > 0)
                {
                    KeyValuePair <string, MemberExpression> kvp = stack.Pop();
                    string           key  = kvp.Key;
                    MemberExpression m    = kvp.Value;
                    Type             type = m.Type;
                    if (type.IsGenericType)
                    {
                        type = type.GetGenericArguments()[0];
                    }

                    TypeRuntimeInfo typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(type);
                    // 检查查询表达式是否显示指定该表关联
                    alias = _aliases.GetJoinTableAlias(typeRuntime.TableName);
                    if (string.IsNullOrEmpty(alias))
                    {
                        // 如果没有,则使用导航属性别名
                        alias = _aliases.GetNavigationTableAlias(key);
                        if (!_navMembers.ContainsKey(kvp.Key))
                        {
                            _navMembers.Add(kvp);
                        }
                    }

                    // 例: a.Client.ClientId
                    if (stack.Count == 0 && !string.IsNullOrEmpty(memberName))
                    {
                        _builder.AppendMember(alias, memberName);
                    }
                }
            }
            else
            {
                // => SelectMany 也会产生类似 'b.Client.Address.AddressName' 这样的表达式
                alias = _aliases.GetTableAlias(expression);
                _builder.AppendMember(alias, memberName);
            }

            // fix issue# Join 表达式显式指定导航属性时时,alias 为空
            return(alias);
        }