You are reading the article Learn How Does Any Work In Linq With Examples? updated in September 2023 on the website Speedmintonvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Learn How Does Any Work In Linq With Examples?
Introduction to LINQ anyWeb development, programming languages, Software testing & others
Syntax
LINQ Any comes in two formats as shown below, let’s see the following syntax,
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.
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 methodIf(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(); 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 methodIn 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:
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 }); { 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,
ExamplesLINQ 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) { 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 }); 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 }); Console.WriteLine("ntUsing LINQ - ANY n"); Console.WriteLine(result_any); Console.ReadKey(); } } }Output:
Recommended ArticlesThis 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 –
You're reading Learn How Does Any Work In Linq With Examples?
Update the detailed information about Learn How Does Any Work In Linq With Examples? on the Speedmintonvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!