private static Table JoinInternal(MetadataHelper helper, string Totable, string ToSchema, Join.JoinTypes JoinType)
        {
            if (helper.Model.PrimaryKey.Columns.Count != 1)
            {
                throw new InvalidOperationException("Only tables with one primary key field is supported");
            }
            MetadataColumn     FromField = helper.Model.PrimaryKey.Columns.First();
            MetadataTable      mt        = helper.Table.Builder.Metadata.FindTable(ToSchema + "." + Totable);
            JoinConditionGroup jcg       = To(helper.Table.Builder, helper.Table, helper.Model, FromField, mt, JoinType, false);

            return(jcg.ToTable());
        }
        private static Table JoinInternal(MetadataHelper helper, string ForeignKeyField, Join.JoinTypes JoinType)
        {
            MetadataColumn FromField = null;

            if (!helper.Model.Columns.TryGetValue(ForeignKeyField, out FromField))
            {
                throw new ArgumentException("The Field " + ForeignKeyField + " was not found", "FromField");
            }
            List <MetadataForeignKey> Fks = new List <MetadataForeignKey>(helper.Model.FindForeignKeys(FromField));

            if (Fks.Count != 1)
            {
                throw new ArgumentException("The Field " + ForeignKeyField + " points to more than one table", "FromField");
            }
            MetadataForeignKey FK      = Fks.First();
            string             table   = FK.ReferencedSchema + "." + FK.ReferencedTable;
            MetadataTable      ToTable = helper.Table.Builder.Metadata.FindTable(table);

            if (ToTable == null)
            {
                throw new InvalidOperationException("The table '" + table + "' was not found in metadata");
            }
            JoinConditionGroup jcg = To(helper.Table.Builder, helper.Table, helper.Model, FromField, ToTable, JoinType, true);

            Table toTable = jcg.ToTable();

            if (FromField.IncludeColumns != null)
            {
                foreach (string include in FromField.IncludeColumns)
                {
                    string iName  = include;
                    string iAlias = null;
                    if (include.IndexOf('=') > 0)
                    {
                        iName  = include.Split('=')[0];
                        iAlias = include.Split('=')[1];
                    }
                    else
                    {
                        iAlias = FromField.Name + "_" + iName;
                    }
                    toTable.Column(iName, iAlias);
                }
            }

            return(toTable);
        }