2
Answers

How to use Max (Linq) in an array with condition?

Hi

This works: counting the number of elements in an array with condition:

 int[] nums = { 1, 2, 2, 3, 3, 4, 5, 6, 5, 7, 7, 7, 8, 8, 9 };

Console.Write(nums.Count(n=>n%2==0)); // count even elements

But how to do the same with Max? 

Console.Write(nums.Maxt(n=>n%2==0)); gives true instead of 8.Why is that and how to fix it?

Thanks

 

Answers (2)