Home > Enterprise >  Getting a number of integers which are more than some value using Aggregate
Getting a number of integers which are more than some value using Aggregate

Time:09-27

I have a task to find a number of integers which are more than some specified value using Aggregate. For example, there is a list of integers

List<int> list = new List<int>() { 100, 200, 300, 400, 500 }; 

On this list I need to find the number of elements of this list which are more than 250. There should be 3 of them, but I need to use Aggregate there somehow to find this number.

Can anybody help please?

CodePudding user response:

You can use Count, which is designed for counting:

 using System.Linq;

 ...

 List<int> list = new List<int>() { 100, 200, 300, 400, 500 };

 ...

 int result = list.Count(item => item > 250);

If you insist on Aggregate:

 int result = list.Aggregate(0, (s, a) => a > 250 ? s   1: s); 
  • Related