Menu ▼



Posted By :
avatarmrkraju
Total Views : 371

LINQ Aggregate function example

LINQ Aggregate function is used to perform calucalation over sequance/list of values. Below examples will show you how to get SUM, MAX, MIN, COUNT using LINQ Aggregate function.

Example

1. Get sum of numbers in int arrary using LINQ Aggregate function.

int[] intary = { 5, 10, 15, 20 };
int intsum = intary.Aggregate((i, j) => i + j);

Output
//50


2. Get max value usage LINQ Aggregate function.

int[] intary = { 21, 150, 5, 120,80,90 };
int maxval = intary.Aggregate((i, j) => i > j ? i : j);

Output
//150


3. Get min value usage LINQ Aggregate function.

int[] intary = { 21, 150, 5, 120,80,90 };
int minval = intary.Aggregate((i, j) => i < j ? i : j);

Output
//5


4. Get count usage LINQ Aggregate function.

int[] intary = { 21, 150, 5, 120,80,90 };
int intCount = intary.Aggregate(0, (i, j) => i = i + 1);

Output
//6







comments powered by Disqus


Search dotnetlearners.com