private DataTable[] OrderTables(DataSet ds)
{
DataTable[] retValue = null;
if (ds == null || ds.Tables.Count == 0)
{
retValue = Array.Empty<DataTable>();
}
else if (TablesAreOrdered(ds))
{
retValue = new DataTable[ds.Tables.Count];
ds.Tables.CopyTo(retValue, 0);
// XDD assumes PArent table exist before its child, if it does not we wont be handle the case
// same as Everett
}
if (null == retValue)
{
retValue = new DataTable[ds.Tables.Count];
List<DataTable> tableList = new List<DataTable>();
// first take the root tables that have no parent
foreach (DataTable dt in ds.Tables)
{
if (dt.ParentRelations.Count == 0)
{
tableList.Add(dt);
}
}
if (tableList.Count > 0)
{ // if we have some table inside;
foreach (DataTable dt in ds.Tables)
{
if (IsSelfRelatedDataTable(dt))
{
tableList.Add(dt);
}
}
for (int readPos = 0; readPos < tableList.Count; readPos++)
{
Debug.Assert(tableList[readPos] != null, "Temp Array is not supposed to reach to null");
foreach (DataRelation r in tableList[readPos].ChildRelations)
{
DataTable childTable = r.ChildTable;
if (!tableList.Contains(childTable))
tableList.Add(childTable);
}
}
tableList.CopyTo(retValue);
}
else
{//there will not be any in case just if we have circular relation dependency, just copy as they are in tablecollection use CopyTo of the collection
ds.Tables.CopyTo(retValue, 0);
}
}
return retValue;
}
private bool IsSelfRelatedDataTable(DataTable rootTable)