User Id :    Password :      New Member   Forgot Password  
How to find min max and avg in an integer array using LINQ in CSharp
Description How to find min max and avg in an integer array using LINQ in CSharp.   No. of Views     4184
  Rating     0
Author Najmul Hoda   Posted On     26 Oct 2010
Tags ADO.NET,C#,LINQ to Object    

Sample Code   Download Code


Sometimes we need to find the minimum, maximum and average in an array of integer.  Using LINQ it become very easy to achieve this functionality.


Suppose you have an array of integer.

int[] arrInteger = new int[] { 23, 20, 17, 102, 1247, 554,32,1 };


To Find Min use the code below.

int min = arrInteger.Min();


var min = (from arrInt in arrInteger 
           select arrInt).Min();


To Find Max use the code below.

int max = arrInteger.Max();


var max = (from arrInt in arrInteger 
           select arrInt).Max();

To Find Average value in the array use the code below.

int avg = arrInteger.Average();


var avg = (from arrInt in arrInteger 
           select arrInt).Average();


Enjoy Coding..................

About Author

About Author He is a team lead, having 8+ years of experience. He loves writing technical article for .Net developers. Najmul Hoda
No Photo
Country United States
Company BirlaSoft
Home Page

Rate this article

Rating options from poor, fair, good, very good to excelent.  


Write your comment here.
Verification Code