Exemplo n.º 1
0
        //public SqlIncluder ThenInclude(MemberExpression member, string alias = null)
        //{
        //    return IncludeInternal(member, alias);
        //}

        private SqlIncluder IncludeInternal(MemberExpression member, string alias = null)
        {
            //检查当前是否Field,是则不再允许Include其他
            if (MemberExpression.Type == ExpressionType.FieldExpression)
            {
                throw new NotSupportedException();
            }
            if (member.Type == ExpressionType.FieldExpression)
            {
                //可以包含多个层级如t.Customer.Region.Name
                //if (!ReferenceEquals(GetTopOnwer(member), MemberExpression))
                //    throw new ArgumentException("Owner not same");
                if (ReferenceEquals(member.Owner, MemberExpression))
                {
                    throw new ArgumentException("Can't include field");
                }
                //判断alias空,是则自动生成eg:t.Customer.Region.Name => CustomerRegionName
                if (string.IsNullOrEmpty(alias))
                {
                    alias = member.GetFieldAlias();
                }
                //TODO:判断重复
                if (Childs == null)
                {
                    Childs = new List <SqlIncluder>();
                }
                var res = new SqlIncluder(this, new SqlSelectItemExpression(member, alias));
                Childs.Add(res);
                return(res);
            }
            else //EntityRef or EntitySet
            {
                Debug.Assert(member.Type == ExpressionType.EntityExpression ||
                             member.Type == ExpressionType.EntitySetExpression);
                //判断Include的Owner是否相同
                //if (!ReferenceEquals(member.Owner, MemberExpression))
                //    throw new ArgumentException("Owner not same");
                if (Childs == null)
                {
                    var child = new SqlIncluder(this, member);
                    Childs = new List <SqlIncluder> {
                        child
                    };
                    return(child);
                }

                var found = Childs.FindIndex(t => t.Expression.Type == member.Type &&
                                             t.MemberExpression.Name == member.Name);
                if (found >= 0)
                {
                    return(Childs[found]);
                }
                var res = new SqlIncluder(this, member);
                Childs.Add(res);
                return(res);
            }
        }