Howto override List<T> Contains

You might want to try using this :

myList.Any(x => x.someProperty == someValue);

from MSDN : http://msdn.microsoft.com/en-us/library/bb534972.aspx

Determines whether any element of a sequence satisfies a condition.

The x => x.someProperty == someValue is called a lambda expression in case you didn't know.

And note that you can use this on anything implementing IEnumerable, so that doesn't restrict you to List<T>.


sounds like you should be doing a Where rather than a Contains

string value = "test";
ac.Where(ac => ac.Name1 == value || ac.Name2 == value);

The reason ac.MyProperty.Contains("Name1") is blowing up is because MyProperty is a List<AnyOtherClass> and not a string


It should be IEquatable<AnyOtherClass> not <string>. You are comparing instances of AnyOtherClass not instances of String, despite the fact that your comparison is actually comparing strings within your class.

But it looks more like what you are trying to do is make some kind of dictionary. In which case you should use the dictionary classes.