Updated April 4, 2023
Introduction to LINQ any
LINQ any is used to check whether the given condition satisfies the sequence of elements. The Any method returns the Boolean value as result and it comes under the System.LINQ.Queryable class. It returns true if any element satisfies the condition, otherwise, it returns false. The LINQ ANY is a quantifier operator in LINQ Query operators. This operator is available only in Method syntax, it does not support query syntax. The main purpose of LINQ ANY is to check whether any single element in the collection satisfies our condition or not.
Syntax
LINQ Any comes in two formats as shown below, let’s see the following syntax,
public static bool Any<TSource>(this IEnumerable<TSource> source);
This is the first overloaded method that takes zero arguments and is only used to check whether the sequence contains any items in the collection or not, depends upon that it returns a bool value as a result.
public static bool Any<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
This is the second overloaded method which takes the predicate method as an argument and is used to check whether any single element in the collection satisfies our given condition or not.
How does any work in LINQ?
The foremost thing in the LINQ ANY operator is to make sure or to check whether any single element in the collection satisfies our condition or not. The operator available only the method syntax, it does not support query syntax. It returns a bool value as a result, it returns true if it satisfies the condition, it returns false once the condition fails. Linq Any available in two formats, let’s see below
- Firstly it checks whether the collection is empty or not.
- Secondly, it checks whether any single element in the collection satisfies our given condition or not.
First overloaded method
This is the first overloaded method that takes zero arguments and is only used to check whether the collection contains any items in the sequence or not. Here in the first method, we explained the collection of items in the product to check whether that collection contains any items or not? Let’s see the following example,
List<ProductMaster> productList=new List<ProductMaster>();
If(productList.Any())
{
Console.WriteLine("Product List has items present in it");
}
Else
{
Console.WriteLine("Empty Product List");
}
It displays Empty Product List as output, it returns bool value, as a result, see the below code to display only the Boolean result,
Var _result= productList.Any(); //no items
Console.WriteLine(_result);
Here it returns either true or false depends on the result. The product list contains no items so it returns false as result.
Second overloaded method
In the second overloaded method, the list contains the product details to check whether any of the product costs have an amount less than 500? We have to check any product in the list contains an amount less than 500 or not we need not to displays any names in it just check the condition and returns the bool value as result, let’s see the below code as follows:
List<Student> students = new List<Student>();
IList<ProductClass> productList = new List<ProductClass>();
productList.Add(new ProductClass { pName = "Speakers", pCost = 2880 });
productList.Add(new ProductClass { pName = "Disk-Drive", pCost = 4000 });
productList.Add(new ProductClass { pName = "KeyBoard", pCost = 1540 });
productList.Add(new ProductClass { pName = "Processor", pCost = 7590 }); productList.Add(new ProductClass { pName = "Monitor", pCost = 3250 });
productList.Add(new ProductClass { pName = "Pendrive", pCost = 475 });
if (productList.Any(p => p. pCost < 500))
{
Console.WriteLine("Products contains amount less that 500");
}
else
{
Console.WriteLine("No less amount products present in the list");
}
It list contains the product cost less than 500 so it displays “Products contains amount less than 500” as output, it returns bool value, as a result, see the below code to display only the Boolean result,
Var _result= productList.Any(p => p. pCost < 500); // condition satisfies Console.WriteLine(_result); // returns true
Here it returns either true or false depends on the result. The product lists contain the cost of the product less than 500 so it returns true as result.
Examples
LINQ Any is used to check whether the given condition satisfies the sequence of elements. This operator is available only in Method syntax, it does not support query syntax. Let’s see the example programmatically,
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_LINQAny
{
class ProductDetails
{
public int pID { get; set; }
public string pName { get; set; }
public int pCost { get; set; }
}
class CustomerDetails
{
public string cName { get; set; }
public string cMobile { get; set; }
public int totalProduct { get; set; }
public int totalCost { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Creating the product details List
IList<ProductDetails> productList = new List<ProductDetails>();
productList.Add(new ProductDetails { pName = "Speakers", pCost = 2880 });
productList.Add(new ProductDetails { pName = "Graphics-Card", pCost = 3000 });
productList.Add(new ProductDetails { pName = "Disk-Drive", pCost = 4000 });
productList.Add(new ProductDetails { pName = "KeyBoard", pCost = 1540 });
productList.Add(new ProductDetails { pName = "Processor", pCost = 7590 });
productList.Add(new ProductDetails { pName = "Monitor", pCost = 3250 });
productList.Add(new ProductDetails { pName = "Pendrive", pCost = 475 });
productList.Add(new ProductDetails { pName = "Pendrive", pCost = 650 });
productList.Add(new ProductDetails { pName = "Pendrive", pCost = 870 });
productList.Add(new ProductDetails { pName = "Desktop-Table", pCost = 1350 });
//customer-product details list
IList<CustomerDetails> customerList = new List<CustomerDetails>();
customerList.Add(new CustomerDetails { cName = "Mithran", cMobile = "9898880901", totalProduct = 3, totalCost = 9880 });
customerList.Add(new CustomerDetails { cName = "Peter", cMobile = "900783221", totalProduct = 2, totalCost = 2190 });
customerList.Add(new CustomerDetails { cName = "Prem", cMobile = "9905003421", totalProduct = 1, totalCost = 4000 });
customerList.Add(new CustomerDetails { cName = "Jhon", cMobile = "8900211056", totalProduct = 4, totalCost = 6120 });
//Using Method Syntax
bool result_any = customerList.Any(c => c.totalCost > 5000);
Console.WriteLine("\n\tUsing LINQ - ANY \n");
Console.WriteLine(result_any);
Console.ReadKey();
}
}
}
Output:
Recommended Articles
This is a guide to LINQ any. Here we discuss the need for a LINQ Any operator with examples by using this operator we can check our conditions easily. You may also have a look at the following articles to learn more –