Using C # Count () with function

I am trying to figure out how many times the maximum value of an array occurs inside an array using

But that did not work. So I tried changing max to an integer to see if this would work, but that didn't work either.

+6
c # max linq predicate
source share
4 answers

You can use the Where function to filter first and then count:

Label1.Text = test.Where(p => p == max).Count().ToString(); 
+7
source share

Using Count(predicate) is fine. You just need to convert the return value (integer) to a string.

 Label1.Text = test.Count(p => p == max).ToString(); 
+16
source share
  int[] test = { 2, 45, 3, 23, 23, 4, 2, 1, 1, 1, 1, 23, 45, 45, 45 }; int count = test.Count(i => i == test.Max()); 

You now have an account, which is your final account. It makes more sense with the int collection. Now, to display it, you can simply call ToString () on the account.

+2
source share

Try something like:

 Label1.Text = test.Where(t => t == test.Max()).Count().ToString(); 
0
source share

All Articles