LINQ (C#) – All() and Any() Method Example and Tutorial

In this tutorial, you will learn:
  1. What are All() and Any() Method in LINQ?
  2. 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() MethodAll() 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.

More Articles

 

Share your thought



PLEASE DISABLE ADBLOCKER AND SUPPORT US!

Please support us by enabling ads on this page. Refresh

YOU DON'T LIKE ADS, WE ALSO DON'T LIKE ADS!
 
But we have to show ads on our site to keep it free and updated. We have to pay huge server costs, domain costs, CDN Costs, Developer Costs, Electricity and Internet Bill. Your little contribution will encourage us to regularly update this site.