- What are
All()
andAny()
Method in LINQ? - Programming Example
All()
and Any()
method are opposite in nature and used to scan whole list against the condition. Both operators scan whole list's items and returns Boolean (True, False) based on the given condition.
All()
Method – All()
method scans whole list and returns true if all elements match the condition. If any of them doesn't fulfil the condition then it returns false.
Condition:
True: If All elements match condition
False: If one or more elements match condition
False: If none of them match condition
Programming Example
using System; using System.Collections.Generic; using System.Linq; namespace LinqTutorial { class ProductStore { public string productName { get; set; } public int productPrice { get; set; } public int inStock { get; set; } } class Program { static void Main(string[] args) { // Creating List IList<ProductStore> productList = new List<ProductStore>(); productList.Add(new ProductStore { productName = "Hard Disk", productPrice = 1280, inStock=40 }); productList.Add(new ProductStore { productName = "Monitor", productPrice = 3000, inStock = 30 }); productList.Add(new ProductStore { productName = "SSD Disk", productPrice = 3500, inStock = 20 }); productList.Add(new ProductStore { productName = "RAM", productPrice = 2450, inStock = 84 }); productList.Add(new ProductStore { productName = "Processor", productPrice = 7680, inStock = 23 }); productList.Add(new ProductStore { productName = "Bluetooth", productPrice = 540, inStock = 45 }); productList.Add(new ProductStore { productName = "Keyboard & Mouse", productPrice = 1130, inStock = 34 }); //LINQ Method Syntax var result = productList.All(p => p.inStock > 10); Console.Write("All Product in Stock: "); Console.WriteLine(result); Console.ReadKey(); } } }
Output:
All Product in Stock: True
_
Any() Method
Any()
Method scans whole list and returns true if any of the elements or all elements match the condition. If none of them match the condition then it returns false.
Condition:
True: If All elements match condition
True: If one or more elements match condition
False: If none of them match condition
Programming Example
using System; using System.Collections.Generic; using System.Linq; namespace LinqTutorial { class ProductStore { public string productName { get; set; } public int productPrice { get; set; } public int inStock { get; set; } } class Program { static void Main(string[] args) { // Creating List IList<ProductStore> productList = new List<ProductStore>(); productList.Add(new ProductStore { productName = "Hard Disk", productPrice = 1280, inStock=40 }); productList.Add(new ProductStore { productName = "Monitor", productPrice = 3000, inStock = 30 }); productList.Add(new ProductStore { productName = "SSD Disk", productPrice = 3500, inStock = 20 }); productList.Add(new ProductStore { productName = "RAM", productPrice = 2450, inStock = 84 }); productList.Add(new ProductStore { productName = "Processor", productPrice = 7680, inStock = 23 }); productList.Add(new ProductStore { productName = "Bluetooth", productPrice = 540, inStock = 45 }); productList.Add(new ProductStore { productName = "Keyboard & Mouse", productPrice = 1130, inStock = 34 }); //LINQ Method Syntax var result = productList.Any(p => p.inStock > 300); Console.Write("All Product in Stock: "); Console.WriteLine(result); Console.ReadKey(); } } }
Output
All Product in Stock: False
_
Summary
In this tutorial, I have tried to explain LINQ All()
and Any()
method with the help of complete programming example. In the next chapter, you will learn LINQ Range Operator.