title: Linq performance: Any vs. Contains
body: <p>This question is related to <a href="https://stackoverflow.com/q/4445219/1163">this one</a>, but not entirely the same <em>I think</em>.</p>
  
  <p>Given:</p>
  
  <pre><code>class Foo
  {
    public string Bar { get; set; }
  }
  ...
  var c1 = new List&lt;Foo&gt;() { ... };
  var c2 = new List&lt;Foo&gt;() { ... };
  </code></pre>
  
  <p>The following 2 loops give the same result:</p>
  
  <pre><code>  foreach (var item in c2.Where(f =&gt; c1.Any(f1 =&gt; f1.Bar.Equals(f.Bar))))
    { ... }
  
    foreach (var item in c2.Where(f =&gt; c1.Select(f1 =&gt; f1.Bar).Contains(f.Bar)))
    { ... }
  </code></pre>
  
  <p>Are they equally fast?  </p>
  
  <p>The difference with <a href="https://stackoverflow.com/q/4445219/1163">the other question</a>, is whether the extra <code>Select</code> statement here, changes the importance of the nature of the underlying collection.  </p>
  
  <p>In other words: does this Contains:  </p>
  
  <pre><code>foos.Contains(foo1)
  </code></pre>
  
  <p>act on the same <em>"kind of collection"</em> as this one:  </p>
  
  <pre><code>foos.Select(f=&gt;f.Bar).Contains(foo1.Bar)
  </code></pre>
  
  <p>My possible -naive- thought could be: "Once we're behind Linq's Select, everything is just  'Lists', so both the Any and Contains are O(n)."</p>