This question is related to this one, but not entirely the same I think.
Given:
class Foo
{
public string Bar { get; set; }
}
...
var c1 = new List<Foo>() { ... };
var c2 = new List<Foo>() { ... };
The following 2 loops give the same result:
foreach (var item in c2.Where(f => c1.Any(f1 => f1.Bar.Equals(f.Bar))))
{ ... }
foreach (var item in c2.Where(f => c1.Select(f1 => f1.Bar).Contains(f.Bar)))
{ ... }
Are they equally fast?
The difference with the other question, is whether the extra Select
statement here, changes the importance of the nature of the underlying collection.
In other words: does this Contains:
foos.Contains(foo1)
act on the same "kind of collection" as this one:
foos.Select(f=>f.Bar).Contains(foo1.Bar)
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)."