title: foreach loop with condition in c#
body: <p>I have this foreach loop in c#, since i am new to c#, i was wondering if this can be written shorter (have the condition part of the loop</p>
  
  <pre><code>foreach (PagerPageFilterInformation filter in PageFilterInformation)
  {
      if (filter.Ancestor == myParent)
      {
          DoSomething()...//Using filter
          break;
      }
  }
  </code></pre>
  
  <p>I tried doing :</p>
  
  <pre><code>PagerPageFilterInformation filter = PageFilterInformation.FirstOrDefault(filter =&gt; filter.Ancestor == myParent);
  if(filter != null)
  {
  DoSomething()...}
  </code></pre>
  
  <p>But it didnt work. could it be because the class:</p>
  
  <pre><code>PagerPageFilterInformation
  </code></pre>
  
  <p>Inherits from the class :</p>
  
  <pre><code>PageFilterInformation
  </code></pre>
  
  <p>?</p>