C# Is the use of Multiple Inner foreach loops Bad Practice.

To demonstrate I will use a super imaginative example:

foreach(Dog d in Dogs)
{
      foreach(Paw p in d.Paws)
      {
         
             foreach(Toe t in p.Toes)
             {
               
                  foreach (ToeNail n in t.ToeNails)
                  {
                        WriteToRepository(d.Id, p.Id, t.Id, n.Id, d.Name, t.condition);
                  }
             }

      }
}

Is there a more elegant method to achive this(without using LINQ)?
XPUSRAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Arabia_vnConnect With a Mentor Commented:
Without using LINQ i couldn't think of anything better than that either :(
0
 
joriszwaenepoelConnect With a Mentor Commented:
Just make sure your "WriteToRepository" method is written very efficiëntly.
Because it is called a lot of times, every performance improvement you make, makes a big difference.

But nested loops are not bad, if the code inside the loop is well written.
0
 
Richard LeeConnect With a Mentor Software EnthusiastCommented:
I don't think there are any other more efficient constructs to achieve that task. Nested loops are not a bad practice, while LINQ provides a logical construct for querying collections.

DaTribe
0
 
tsellsConnect With a Mentor Commented:
I would also recommend once you implement solutions like this to use a tracing program like dottrace to verify how much time is being spent in the methods.  This will allow you to see just how efficient (or inefficient) your methods are in real time.  
0
All Courses

From novice to tech pro — start learning today.