ISerialized .Net, C#, Scrum and agile software development

17Sep/100

Linq performance, Count() vs Any()

Posted by Pål Eie

I just came across some very interesting information. Every used the following statement:

if (mysequenece.Count() > 0)
{
    //Do something
}

The problem here is the Count() statement which can potentially be very inefficient, as it traverse the whole list to calculate a value. But we reallly don't need the exact count, in most cases we just need to know if there are any values, and hence, we can rather use Any() which is another extension method from Linq.