What is the difference between Contains and Any in LINQ?
Contains cares about whether the source collection is an ICollection, Any does not.
Enumerable.Contains http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs#f60bab4c5e27a849
public static bool Contains(this IEnumerable source, TSource value)
{
ICollection collection = source as ICollection;
if (collection != null)
{
return collection.Contains(value);
}
return source.Contains(value, null);
}
Enumerable.Any http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs#6a1af7c3d17845e3
public static bool Any(this IEnumerable source, Func predicate)
{
foreach (TSource local in source)
{
if (predicate(local))
{
return true;
}
}
return false;
}