title: FilterAndCast Linq Extension Method
body: <p>I have some places where I do something like this:</p>
  
  <pre><code>var listOfBaseClasses = new List&lt;BaseClass&gt;() { ... }
  var things = listOfBaseClasses
      .Where(t =&gt; t is Thing)
      .Cast&lt;Thing&gt;()
      .ToList()
  </code></pre>
  
  <p>I realize this is a bit of a code smell, such is life when supporting someone else's code.</p>
  
  <p>I would like to create an extension method to combine the Where and Cast extension methods. This is what I have so far..</p>
  
  <pre><code>public static IEnumerable&lt;TResult&gt; FilterAndCast&lt;TResult&gt;(this IEnumerable source)
  {
      if (source == null)
      {
          throw new ArgumentNullException("source");
      }
  
      foreach (var item in source)
      {
          if (item.GetType() == typeof(TResult))
          {
              yield return (TResult)item;
          }
      }
  }
  </code></pre>
  
  <p>Is there a better way to do this?</p>