title: Which is quickest, Loop or FirstOrDefault()
body: <p>I'm trying to improve performance on a WPF app as my users are saddened by the fact that one part of the system, seems to have a performance issue. The part in question is a screen which shows logged in users. The slow part logs them out, by: scanning in their employee ref and finds their child control and removes it from the parent, i.e. logs them out. This currently uses a loop.</p>
  
  <pre><code>foreach (var userControl in UsersStackPanel.Children)
  {
      if (userControl.Employee.EmpRef == employee.EmpRef)
      {                  
          // plus some DB stuff here
          UsersStackPanel.Children.Remove(userControl);                              
          break;
      }
  }
  </code></pre>
  
  <p>but I've an alternative which does this, </p>
  
  <pre><code>var LoggedInUser = (UI.Controls.Generic.User)UsersStackPanel.Children
                         .OfType&lt;FrameworkElement&gt;()
                         .FirstOrDefault(e =&gt; e.Name == "EmpRef" + employee.EmpRef);
  if (LoggedInUser != null)
  {
      // some DB stuff here
      UsersStackPanel.Children.Remove(LoggedInUser);
  
  }
  </code></pre>
  
  <p>I've timed both using the Stopwatch class but the results don't point to which is better, they both return 0 miliseconds. I am wondering if the DB part is the bottle neck, i just thought I'd start with the screen to improve things there as well as the DB updates. 
  Any thoughts appreciated.
  Dan</p>