If you want to use LinkedList.Contains, you can do that, but Foo
but implement IEquatable<Foo>
. LinkedList.Contains does not work via a Predicate function, but rather by searching for a specific element. To use Contains, you would write:
bool contains = list.Contains(foo);
However, in this case, you may want to consider using the Enumerable.Any() extension method instead of Contains(). Doing this, it will look like your previous code, except you don't need the first "foo":
Foo foo = new Foo(someString);
LinkedList<Foo> list = new LinkedList<foo>();
// Populate list with Foos
bool contains = list.Any(x => foo.Bar == x.Bar);
Since "foo" is visible in the current scope, when you create the lambda expression, the compiler will automatically generate a closure over the "foo" variable, allowing you to use it directly. You only need to specify the argument name (x) for use in the predicate function created in the lambda.
This requires a reference to System.Core.dll and a using System.Linq;
at the top of your file, as well as .NET 3.5+.