Any() vs Count() in LINQ is an important comparison for C# developers looking to write efficient queries. In this article, we’ll explore the differences between these two methods, go through practical examples, and learn when to use each in real-world scenarios. We’ll also take a real-time example to see which one delivers better performance, review the SQL queries generated for each, and share essential performance tips.
When working with collections in C#, especially in database-driven applications, you’ll often need to check if elements exist or determine how many there are. This is where the comparison of Any() vs Count() in LINQ becomes important. Although both methods may seem similar, the way they execute—and their impact on performance—can be significantly different. Choosing the right one can help you write faster, cleaner, and more efficient queries, particularly when working with large datasets or Entity Framework Core.
Introduction: Understanding LINQ Collection Checks
How Any() Works in LINQ and When to Use It
Apply Any() when the requirement is simply to verify whether at least one element exists in the collection or matches a condition.
if (orders.Any(o => o.Status == "Pending"))
{
...
}
Advantage:
Evaluates only until the first matching element is found, resulting in faster execution, especially for large datasets or deferred LINQ queries (e.g., Entity Framework). Evaluates only until the first matching element is found, resulting in faster execution, especially for large datasets or deferred LINQ queries (e.g., Entity Framework).
SQL Generated by EF Core Any Method
SELECT CASE
WHEN EXISTS (
SELECT 1
FROM [Orders] AS [o]
WHERE [o].[Status] = N'Pending'
)
THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END
Execution:
- Stops as soon as one matching record is found.
- Minimal I/O and CPU usage.
- Ideal for large datasets.
Learn more: Microsoft documentation for Any()
How Count() Works in LINQ and Its Common Use Cases
Choose Count() when you need to determine the exact number of elements that satisfy a given condition.
int pendingOrderCount = orders.Count(o => o.Status == "Pending");
Advantage:
Returns precise counts, which is essential for reporting, analytics, or business rules that rely on exact quantities.
SQL Generated by EF Core for Count method
SELECT COUNT(*)
FROM [Orders] AS [o]
WHERE [o].[Status] = N'Pending'
Execution:
- Scans all matching records before returning a result.
- More rows = more time, even if you just need existence.
- Inefficient for large tables.
Learn more: Microsoft documentation for Count()
Any() vs Count() in LINQ – Understanding the Core Differences
Any()
is optimized for checking existence without scanning the whole collection.Count()
scans the entire collection unless the source has a directCount
property (likeList<T>
).
Common Mistakes Developers Make with Any() and Count()
❌ Bad Practice
if (collection.Count() > 0)
{
// Do something
}
Why it’s bad: Iterates through the entire collection just to check if it’s not empty. In large datasets or deferred queries (like EF Core), this leads to unnecessary processing and slower performance.
✅ Good Practice
if (collection.Any())
{
// Write your logic
}
Why it’s good: Stops as soon as it finds the first matching element, making it much faster for large datasets and more efficient for deferred queries.
💡 Best Practices for Using Any() and Count() in LINQ
- Use
Any()
for existence checks—it’s faster, more efficient, and translates into an optimizedEXISTS
query in EF Core. - Avoid
Count() > 0
unless you specifically need the total number of elements.
Summary:
Learn the performance differences between Any()
and Count()
in LINQ, and discover the most efficient way to check for data in collections. This guide highlights best practices, common pitfalls, and how the right choice can improve query speed, especially in EF Core.
Check out my other posts 
- Code Review Checklist for Clean, Secure, and Maintainable Code
- .NET Coding Best Practices Every Developer Should Follow
- LINQ Methods in .NET 9 and C# 13 : Index, CountBy, and AggregateBy
- Best Practices for Naming Boolean Variables in C# for Cleaner Code
- How to Implement Scrum Methodology in Agile Project Management