Skip to main content

How to Check Whether Every Item Matches with LINQ All in C#

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

lows.All(low => low > 0) returns True only when every item passes the test, here when every temperature in lows is above 0. It stops at the first item that fails, and it returns True for an empty array.

Microsoft's reference: Determines whether all elements of a sequence satisfy a condition. It returns true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false. The predicate is the test you pass in: A function to test each element for a condition.

All doesn't need to see every item to answer False, because one item that fails is enough. The reference: The enumeration of source is stopped as soon as the result can be determined.

Syntax​

Syntax
bool <result> = <items>.All(<item> => <condition>);
PartWhat it is
<items>Any sequence, such as an array or a List<T>
<item> => <condition>The test, written as a lambda: <item> names one item, and <condition> is a true or false expression about it, such as low => low > 0
<result>true when every item passes, or when there are no items; false as soon as one item fails

The Example​

A console app called LinqAll has the lowest temperature of each day of a week. It asks whether every low is above 0, then whether every low is above 3 while it counts how many lows All tests, and last asks the same of an empty array.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n LinqAll
cd LinqAll
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/LinqAll/LinqAll.csproj:
Determining projects to restore...
Restored /Users/you/source/LinqAll/LinqAll.csproj (in 79 ms).
Restore succeeded.

dotnet new console -n LinqAll creates a folder named LinqAll with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or LinqAll.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the temperatures and the first question:

Program.cs (every low above 0)
// The lowest temperature of each day of a week, in degrees Celsius.
int[] lows = [4, 7, 2, 9, 5, 6, 3];

// All returns True only when every item passes the test.
Console.WriteLine($"Every low above 0: {lows.All(low => low > 0)}");

low => low > 0 is the test. All calls it with each item in turn, in low, and returns True only if it returned true for every item.

Then the question with 3, and a test that counts how often it runs:

Program.cs (count the tests)
// All stops at the first item that fails. This test counts how often it runs.
int tested = 0;
bool aboveThree = lows.All(low =>
{
tested++;
return low > 3;
});
Console.WriteLine($"Every low above 3: {aboveThree}, after testing {tested} of the {lows.Length} lows");

This lambda has a body in braces: tested++ adds 1 each time it runs, and return low > 3; gives the answer. The count shows when All stopped.

Last, an empty array:

Program.cs (an empty array)
// An empty array has no item that fails, so All returns True.
int[] noLows = [];
Console.WriteLine($"Every low in an empty array above 3: {noLows.All(low => low > 3)}");

[] creates an array with no items.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Every low above 0: True
Every low above 3: False, after testing 3 of the 7 lows
Every low in an empty array above 3: True

macOS-style terminal window titled Terminal — LinqAll showing dotnet run printing Every low above 0: True, then Every low above 3: False, after testing 3 of the 7 lows, then Every low in an empty array above 3: True

Every low is above 0, so the first answer was True. For above 3, All returned False after testing only 3 of the 7 lows: the third low, 2, failed, and the four lows after it were never tested. The empty array gave True, because no item failed the test. That's the or if the sequence is empty part of the reference quoted above.

The Complete File​

macOS-style code window titled Program.cs — LinqAll showing an array of seven low temperatures, All with the test low above 0, All with a test in braces that counts its calls and checks low above 3, the result printed with the count, and All called on an empty array

Program.cs
// The lowest temperature of each day of a week, in degrees Celsius.
int[] lows = [4, 7, 2, 9, 5, 6, 3];

// All returns True only when every item passes the test.
Console.WriteLine($"Every low above 0: {lows.All(low => low > 0)}");

// All stops at the first item that fails. This test counts how often it runs.
int tested = 0;
bool aboveThree = lows.All(low =>
{
tested++;
return low > 3;
});
Console.WriteLine($"Every low above 3: {aboveThree}, after testing {tested} of the {lows.Length} lows");

// An empty array has no item that fails, so All returns True.
int[] noLows = [];
Console.WriteLine($"Every low in an empty array above 3: {noLows.All(low => low > 3)}");

Common Mistakes​

MistakeWhat happensDo this instead
Calling All without a testlows.All() didn't build: error CS1501: No overload for method 'All' takes 0 argumentsPass the test as a lambda: lows.All(low => low > 0)
Writing the condition without item =>lows.All(low > 3) didn't build: error CS0103: The name 'low' does not exist in the current contextName the item, then write =>: lows.All(low => low > 3). low exists only inside the lambda, as the name before =>
Expecting False for an empty listnoLows.All(low => low > 3) printed True, as the run showedWhen an empty list should fail, check that it has items first: noLows.Any() && noLows.All(low => low > 3) printed Has lows, and every low above 3: False

FAQ​

Does All return the items that pass the test?​

No, only True or False. The reference: This method does not return all the elements of a collection. Instead, it determines whether all the elements of a collection satisfy a condition. To get the items that pass, use Where: lows.Where(low => low > 3) printed Lows above 3: 4, 7, 9, 5, 6.

Does a List have its own version of All?​

Yes, TrueForAll. The reference: Determines whether every element in the List<T> matches the conditions defined by the specified predicate. On a List<int> of the same lows, lows.TrueForAll(low => low > 0) printed TrueForAll above 0: True, and lows.TrueForAll(low => low > 3) printed TrueForAll above 3: False. Like All, it returns true for an empty list: If the list has no elements, the return value is true. All works on any sequence, such as the array in the example.

Conclusion​

items.All(item => condition) returns True when every item passes the test, and False as soon as one item fails, without testing the rest. For an empty sequence it returns True, so add Any() first when an empty list should count as a failure. All answers only True or False; Where gives you the items that pass.

Additional Resources​