示例#1
0
        /// <summary>
        /// 通过左实体数据源和右实体数据源,来找到它们之间的第一个的引用属性,用以构造一个连接。
        /// </summary>
        /// <param name="left">拥有引用关系的左数据源。</param>
        /// <param name="right">右实体数据源。</param>
        /// <returns></returns>
        public IJoin Join(
            ITableSource left,
            ITableSource right
            )
        {
            //通过实体的对比,找到从左实体到右实体之间的第一个的引用属性。
            var rightEntity = right.EntityRepository.EntityType;
            var properties  = left.EntityRepository.EntityMeta.ManagedProperties.GetCompiledProperties();

            for (int i = 0, c = properties.Count; i < c; i++)
            {
                var refProperty = properties[i] as IRefEntityProperty;
                if (refProperty != null && refProperty.RefEntityType == rightEntity)
                {
                    var condition = this.Constraint(
                        left.Column(refProperty.RefIdProperty),
                        right.Column(Entity.IdProperty)
                        );

                    var joinType = refProperty.Nullable ? JoinType.LeftOuter : JoinType.Inner;

                    return(Join(left, right, condition, joinType));
                }
            }

            throw new InvalidProgramException(string.Format(
                                                  "没有从 {0} 到 {1} 的引用关系,请指定具体的对比条件。", left.GetName(), right.GetName()
                                                  ));
        }