LINQ Not Any vs All Don't
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In LINQ, !Any(predicate) and All(x => !predicate(x)) are logically equivalent for finite sequences, but they are not always equally readable. Choosing the clearer form matters in real code, especially when predicates are already negative. Understanding empty-sequence behavior also prevents subtle validation bugs.
Core Sections
Logical relationship between the two forms
For a predicate p, these are equivalent:
!source.Any(p)source.All(x => !p(x))
Both mean “there is no element that satisfies p.”
Example:
Readability guidelines
Even though equivalent, one form can be easier to understand:
- use positive
Allwhen domain statement is naturally universal - use
!Anywhen domain statement is naturally existential absence
Example of clearer expression:
orders.All(o => o.IsApproved)is often clearer than!orders.Any(o => !o.IsApproved)
Prefer fewer negations in code paths that are already complex.
Empty sequence behavior
Important semantics:
Anyon empty sequence returnsfalseAllon empty sequence returnstrue
So for empty sequences, both forms still match:
!Any(p)becomestrueAll(not p)alsotrue
This is mathematically consistent but can surprise developers expecting explicit non-empty validation.
If non-empty requirement exists, combine checks explicitly.
Performance characteristics
Both methods short-circuit:
Anystops at first matching itemAllstops at first non-matching item
In practice performance is similar for equivalent predicates. Choose based on clarity, not micro-optimization assumptions.
Avoid double-negative predicates
Code gets hard to read when domain condition is already negated.
Hard to parse:
Clearer:
Express business rule directly whenever possible.
Complex predicates and helper methods
If predicate is long, extract helper methods rather than nesting negations inline.
This improves testability and reduces cognitive load in reviews.
Practical testing strategy
For code that uses either form, include tests for:
- empty sequence
- one matching element
- one non-matching element
- mixed collection
These cases catch most logic misunderstandings quickly.
Query provider considerations
When using LINQ with query providers such as Entity Framework, translation of Any and All can differ in generated SQL shape. Semantics remain equivalent, but execution plans may vary based on indexes and predicate complexity. If performance matters for large datasets, inspect generated SQL and actual query plans rather than assuming identical runtime cost.
Team readability conventions
Set one convention in your codebase and apply it consistently. For many teams, positive All expressions are easier to review than nested negations. Whatever convention you choose, document it in style guidelines so code reviews focus on business logic instead of repeated syntax debates.
This consistency also improves static analysis rules and auto-review tooling, because patterns become predictable and easy to detect.
It also reduces reviewer fatigue in large PRs.
Establish a team style note for predicate polarity so reviewers can quickly spot logic inversion risks in Any, All, and ! combinations.
Common Pitfalls
- Assuming
Allreturns false for empty sequences. - Writing nested negations that hide business intent.
- Using one form for style preference without considering readability.
- Forgetting to add non-empty check when domain requires at least one element.
- Refactoring predicates without preserving logical equivalence.
Summary
!Any(p)andAll(not p)are equivalent in LINQ for finite sequences.- Prefer the expression that states business intent most clearly.
- Remember both forms return true on empty sequences.
- Add explicit non-empty checks where required by domain rules.
- Keep complex predicates readable with helper methods and targeted tests.
Related reading

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.