title: Remove foreach - c# code-optimization
body: <p>How to optimize this code?</p>
  
  <p>ParentDoglist, ChildDoglistis - Ilist. dogListBox - List Box</p>
  
  <pre><code>foreach (Dog ParentDog in ParentDoglist)
  {
   foreach (Dog ChildDog in ChildDoglist)
   {
    if(ParentDog.StatusID==ChildDog.StatusID)
    dogListBox.Items.Add(new ListItem(ParentDog.Name, ParentDog.Key));
   }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> 
  ParentDogTypeList, DogTypeList were renamed as ParentDoglist,ChildDoglist, where both are  not related with each other</p>
  
  <pre><code>if(ParentDog.Key==ChildDog.Key)
  </code></pre>
  
  <p>was changed to </p>
  
  <pre><code>if(ParentDog.StatusID==ChildDog.StatusID)
  </code></pre>
  
  <p><strong>Complete Story:</strong></p>
  
  <p>I need to populate a drop down which would reciprocate a Parent Child relationship. There are certain dogs which may not have any child and that would be called as leafdog. And I also need to show the number of dogs in that particular category</p>
  
  <p>DD would look like </p>
  
  <pre><code>Parent1
    Child11 (10)
    Child12 (12)
  Parent2
    Child21 (23)
    Child22 (20)
  Leaf1 (20)
  Leaf2 (34)
  </code></pre>
  
  <p>So, the ParentDoglist would bring all the Child and leaf elements along with the count and ChildDogList would have the Parent and leaf ID's hence I would be able to populate the respective Child to their Parent and bind the leaf directly.</p>
  
  <p>The Parent, Child and Leaf Dog would be maintain in one table and differentiated by statusid and count would be in another table. </p>
  
  <p>No parent would have any count, only child and leaf would have count</p>
  
  <p><strong>Table Schema:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/UFfFZ.jpg" alt="alt text"></p>