internal DataTable[] TopLevelTables(bool forSchema)
{
// first let's figure out if we can represent the given dataSet as a tree using
// the fact that all connected undirected graphs with n-1 edges are trees.
List<DataTable> topTables = new List<DataTable>();
if (forSchema)
{
// prepend the tables that are nested more than once
for (int i = 0; i < Tables.Count; i++)
{
DataTable table = Tables[i];
if (table.NestedParentsCount > 1 || table.SelfNested)
{
topTables.Add(table);
}
}
}
for (int i = 0; i < Tables.Count; i++)
{
DataTable table = Tables[i];
if (table.NestedParentsCount == 0 && !topTables.Contains(table))
{
topTables.Add(table);
}
}
return topTables.Count == 0 ?
Array.Empty<DataTable>() :
topTables.ToArray();
}