SoFunction
Updated on 2025-03-06

The difference between C# Any and AII methods

Another type of query we often need is to determine whether the data meets a certain condition, or to ensure that all data meets a certain condition. For example, it is necessary to determine whether a product has been out of stock (stock is 0), or whether a transaction has occurred.

LINQ provides two Boolean methods: Any() and All(), which quickly determine whether a condition is true or false for data. Therefore, the data is easily found, as shown in the example below.

Follow the steps below to create an example in Visual Studio:

(1) Create a new console application.

(2) Create the code of the Customer class and the code of initializing the customer list (List<Customer> customers).

(3) In the Main() method, after the customers list is initialized and query declaration, delete the processing loop and enter the code shown below:

bool anyUSA = (c =>  == "USA"); 
if (anyUSA)
{
   ("Some customers are in the USA");
}
else
{
   ("No customers are in the USA");
}
bool allAsia = (c =>  == "Asia"); 
if (allAsia)
{
   ("All customers are in Asia");
}
else
{
   ("Not all customers are in Asia");
}

(4) Compile and execute the program and you will see some messages pointing out that some customers are from the United States, and not all customers are from Asia:

Some customers are in the USA 
Not all customers are in Asia
Program finished, press Enter/Return to continue:

Description of examples

The Customer class and the Customers list are initialized the same as in the previous example. In the first query statement, the Any() method is called to check whether the Customer Country field's value is USA with a simple Lambda expression:

bool anyUSA = (c =>  == "USA");

The LINQ method Any() applies the Lambda expression c=>=="USA" sent to it to all data in the customers list. If the Lambda expression is true for any customers in the list, it returns true.

Then check the Boolean result variable returned by the Any() method, output a message, displaying the result of the query. Although the Any() method only returns true or false, it will execute a query and get true or false results):​​​​​​​​​

if (anyUSA)
{
   ("Some customers are in the USA");
}
else
{
   ("No customers are in the USA");
}

Although this message can be made more compact with some clever code, the code here is more intuitive and easy to understand. AnyUSA is set to true, because there are indeed customers in the United States in the dataset, so I saw the message Some customers are in the USA.

In the next query statement, the AII() method is called to use another simple Lambda expression to determine whether all customers are from Asia:

bool allAsia = (c=>  =="Asia");

The LINQ method All() applies the Lambda expression to the dataset and returns false because some customers are not from Asia. Then return the corresponding message according to the value of allAsia.

This is the end of this article about the difference between C# Any() and AII() methods. For more related C# Any() and AII() content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!