private Account Increment() { while (SortedAccountsI.Any() && SortedAccountsI.Last().Current == null) { SortedAccountsI.PopBack(); if (!AccountsList.Any()) { throw new InvalidOperationException("accounts list is empty"); } AccountsList.PopBack(); } if (!SortedAccountsI.Any()) { return(null); } else { Account account = SortedAccountsI.Last().Current; SortedAccountsI.Last().Increment(); // If this account has children, queue them up to be iterated next. if (!FlattenAll && account.Accounts.Any()) { PushBack(account); } // Make sure the sorting value gets recalculated for this account account.XData.SortCalc = false; return(account); } }