In LINQ, Don't Use Count() When You Mean Any()

Posted by on in Blogs
If you have a list, array, or query in a C#/LINQ application and need to check and see if the list is empty, the correct way to do this is to use the Any() extension method:

if (q.Any())

Similarly, you can check to see if any elements in the list meet a certain condition:

if (q.Any(i => i.IsSpecial))

If the query provider is something like LINQ to Entities, this will be translated into fairly efficient SQL using EXISTS.

For some reason, I see a lot of people write this code using the Count() extension method instead (maybe they don't know about Any()?), like this:

if (q.Count() > 0)

This is wrong for two reasons:

  • It's imperative instead of expressive. Using Any() tells the query provider, "Please determine if the list is non-empty using the most efficient way you can." Using Count() > 0 tells the query provider, "Please do exactly what I tell you, regardless of what I really need." Because LINQ is intended to support a variety of query providers, it is important to be expressive instead of imperative, and to let the provider specialize the implementation.

  • Because we don't actually care about the exact count of items in the list, only that it is greater than zero, using the Count() function can cause the provider to do considerably more work than necessary. Consider a linked list, or a SQL-based provider.


  • Guest
    Olaf Monien Wednesday, 21 April 2010

    Not using Count() if you don't actual need the exact number, is very important as you lined out - agreed.

    "It’s imperative instead of expressive." is itself not a reason against using Count() imho, as this is very simplistic example only. There are real world situations, where you have to count or - more abstract - where you have to compare two values - even in expressive LINQs

    Response: If you actually need an exact count and are prepared to pay the performance cost of getting it, then by all means feel free to use .Count(). My point regarding expressivity is that when what you really need is .Any(), then you probably don't need an exact count. FWIW, people make this same mistake in SQL all the time, and it's wrong there for the same reasons it's wrong in LINQ.

  • Guest
    Aaron Murray Wednesday, 30 November 2011

    Agreed - this is a great post for anyone who is concerned about the performance of generated SQL using LINQ.

  • Guest
    Ray Akkanson Thursday, 8 December 2011

    Can we use 'Any' for null objects?

    Ray Akkanson

  • Guest

    [...] استفاده از Any به جای Count وقتی منظورتون همون Any است. [...]

  • Guest
    Ray Akkanson Friday, 9 March 2012

    Thanks. This is very useful information.

    Ray Akkanson

  • Guest
    Robert Fryca Sunday, 12 May 2013

    Compare sql generated by EF 5.0.

    [GroupBy1].[A1] AS [C1]
    COUNT(1) AS [A1]
    FROM [dbo].[Members] AS [Extent1]
    WHERE [Extent1].[Uguid] = @p__linq__0

    CASE WHEN ( EXISTS (SELECT 1 AS [C1] FROM [dbo].[Members] AS [Extent1] WHERE [Extent1].[Uguid] = @p__linq__0)) THEN cast(1 as bit)
    WHEN ( NOT EXISTS (SELECT 1 AS [C1] FROM [dbo].[Members] AS [Extent2] WHERE [Extent2].[Uguid] = @p__linq__0)) THEN cast(0 as bit) END AS [C1]
    FROM ( SELECT 1 AS X )

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: