I am sitting now for a few hours on the following problem, I have a code, that calculates how many boxes fit in a package. For that I calculate package per package and check box for box.
Here is the code:
private bool CalculateOnePackage(Package package, List<Item> workItems, List<Item> packagedItemsList)
{
  if (workItems.Count == 0)
  {
    return true;
  }
  var packages = new List<Package>();
  Item tempItem = null;
  foreach (Item entity in workItems)
  {
    if (/* execude code that changes entity and packages */)
    {
      tempItem = entity;
      break;
    }
  }
  if (tempItem == null)
  {
    return false;
  }
  packagedItemsList.Add(tempItem);
  workItems.Remove(tempItem);
  foreach (var p in packages)
  {
    this.CalculateOnePackage(p, workItems, packagedItemsList);
  }
  return true;
}
How can I rewrite the code to only use loops? We have the problem, that this code causes a StackoverflowException.
 
     
    