+92 332 4229 857 99ProjectIdeas@Gmail.com

LINQ Any() Method (C#.net)



LINQ Any() Method (C#.net):
The following code shows you how to check any value in your array for some specific operation. For example, array contains {2,1,5,7,8} and you apply LINQ Any() method and queries that any of element in that array is divided by 2. Then if you see there are 2 and 8 which is divided by two so query returns True else if not true then returns false. See the code below where it is checked every element remainder equals zero.


Code

int[] number = { 6, 8, 9, 5, 7 };

bool result = number.Any(x => x % 2 == 0);

Console.Write(result);

Output of the program is:
True (Because if we take 6 and 8 mod with 2, the answer is zero )

Another Example:

List<string> words = new List<string> { "osama","saad","shizzi","bilal","ahsan" };

var res = words.Any(item => item.StartsWith("h"));

Console.Write(res);

Output of the program is:
False (Because no string is starting with letter 'h')

0 comments: