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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Arabia_vnCommented:
Without using LINQ i couldn't think of anything better than that either :(
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
joriszwaenepoelCommented:
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 LeeSoftware 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
tsellsCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.