Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use Any method instead of Count? [duplicate]

Possible Duplicate:
Which method performs better: .Any() vs .Count() > 0?

I just wonder why should I use Any() instead of Count()?, if we took the msdn example :

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}
class Person
{
    public string LastName { get; set; }
    public Pet[] Pets { get; set; }
}

public static void AnyEx2()
{
    List<Person> people = new List<Person>
        { new Person { LastName = "Haas",
                       Pets = new Pet[] { new Pet { Name="Barley", Age=10 },
                                          new Pet { Name="Boots", Age=14 },
                                          new Pet { Name="Whiskers", Age=6 }}},
          new Person { LastName = "Fakhouri",
                       Pets = new Pet[] { new Pet { Name = "Snowball", Age = 1}}},
          new Person { LastName = "Antebi",
                       Pets = new Pet[] { }},
          new Person { LastName = "Philips",
                       Pets = new Pet[] { new Pet { Name = "Sweetie", Age = 2},
                                          new Pet { Name = "Rover", Age = 13}} }
        };

    // Determine which people have a non-empty Pet array.
    IEnumerable<string> names = from person in people
                            where person.Pets.AsQueryable().Any()
                            select person.LastName;

    foreach (string name in names)
        Console.WriteLine(name);

    /* This code produces the following output:

       Haas
       Fakhouri
       Philips
    */
}

What if I used :

  IEnumerable<string> names = from person in people
                            where person.Pets.Count() > 0
                            select person.LastName;

It will give same result ! , (I don't think it created for shortness or something) , is there any feature for Any() ??

like image 430
Mohamed Farrag Avatar asked Sep 26 '12 13:09

Mohamed Farrag


2 Answers

Depending on exactly what implementation of IEnumerable<> is hiding behind the interface, Any could potentially be vastly quicker than Count. If for example there's actually LINQ-to-SQL, or some other database provider there, it could be the difference between checking a table for at least 1 record, or having to count every record in the database.

However, to my mind, the much more important reason is that using Any() expresses your INTENT better than checking for Count() > 0. It asks "are there any items?" rather than "find out how many items there are. Is that number greater than zero". Which to you is the more natural translation of "are there any items?" ?

like image 126
AakashM Avatar answered Oct 17 '22 01:10

AakashM


Any just checks if the sequence contains at least one element, while Count needs to iterate over all elements. That's the difference. A classic scenario where Any is preferred over Count is this:

if (sec.Count() > 0)

vs

if (sec.Any())
like image 25
Claudio Redi Avatar answered Oct 17 '22 02:10

Claudio Redi