Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

All, Any and Aggregate methods in LINQ

Author: Rudy Pinson

In this article, we will learn how to use All, Any and Aggregate methods in LINQ.

 

 

In previous article, we have learned to use Take/TakeWhile and Skip/SkipWhile Operators in LINQ. We will learn some more methods of LINQ in this article. All these methods are part of the “System.Linq” namespace in ASP.NET. 
 
Any

“Any” method is used to check whether any element in the collection matched the mentioned condition. It returns Boolean value true if any element matched to the condition and it return false if no element matched to the given condition. This functionality can be done with loop construct in ASP.NET but “Any” method reduces the code. If we use “Any” method without any condition it will check whether any element exists in the collection or collection is empty.

Example:

C#

string[] strings = { };
bool result = strings.Any();
 
VB.NET

Dim strings As String() = {}
Dim result As Boolean = strings.Any()

The result will be:
False
 
Example:
 
C#

int[] numbers = { 8, 3, 6, 9, 4, 2, 1, 7, 0, 5 };
bool result = numbers.Any(x => x > 5);
bool result2 = numbers.Any(x => x == 1);
 
VB.NET

Dim numbers As Integer() = {8, 3, 6, 9, 4, 2, 1, 7, 0, 5}
Dim result As Boolean = numbers.Any(Function(x) x > 5)
Dim result2 As Boolean = numbers.Any(Function(x) x = 1)
 
The result will be:
True
True
 
First example shows “Any” method without any condition. It will check that there is any string in “strings”. Second example shows “Any” method with condition. First “Any” method will check whether there is any element greater than 5.  Second “Any” method will check number 1 in “numbers”.
 
All

“All” method is used to check that all elements satisfy given condition. It returns Boolean value true if all elements in the collection satisfy the condition and returns false if any of the elements in the collection fails to satisfy the condition.

Example:
 
C#

int[] numbers = { 8, 3, 6, 9, 4, 2, 1, 7, 0, 5 };
 
bool result = numbers.All(x => x <= 9);
bool result2 = numbers.All(x => x > 3);
 
VB.NET

Dim numbers As Integer() = {8, 3, 6, 9, 4, 2, 1, 7, 0, 5}
Dim result As Boolean = numbers.All(Function(x) x <= 10)
Dim result2 As Boolean = numbers.All(Function(x) x > 3)
 
First “All” method will iterate through all the collection and check that all elements are less than and equal to 9. Second “Any” method will check that all elements are greater than 3.

The result will be:
True
False
 
Sum

“Sum” method calculate sum of all the elements in the collection and returns a single value.

C#

int[] numbers = { 8, 3, 6, 9, 4, 2, 1, 7, 0, 5 };
int result = numbers.Sum();
 
VB.NET
 
Dim numbers As Integer() = {8, 3, 6, 9, 4, 2, 1, 7, 0, 5}
Dim result As Integer = numbers.Sum()
 
The result will be:
45
 
Aggregate

“Aggregate” method is more flexible method than “Sum”.  The result of the previous elements is used with next element for further result according to condition. We can use different operators and there combinations for calculation in “Aggregate” method.
 
Example:

C#
 
int[] numbers = { 8, 3, 6, 9, 4, 2, 1, 7, 5 };
int result = numbers.Aggregate((x,y) => x + y);
 
VB.NET
 
Dim numbers As Integer() = {8, 3, 6, 9, 4, 2, 1, 7, 5}
Dim result As Integer = numbers.Aggregate(Function(x, y) x + y)
 
The result will be:
45
 
Example:
 
C#
 
int[] numbers = { 8, 3, 6, 9, 4, 2, 1, 7, 5 };
int result2 = numbers.Aggregate((x, y) => x * y);
 
VB.NET
 
Dim numbers As Integer() = {8, 3, 6, 9, 4, 2, 1, 7, 5}
Dim result2 As Integer = numbers.Aggregate(Function(x, y) x * y)
 
The result will be:
362880
 
Second “Aggregate” method will multiply first two elements than the result will be multiplied to next element and the process continues until the last element of the collection.
 
Average
 
“Average” method calculates the average of elements in the numeric collection with very short code.
 
Example:
 
C#
 
int[] numbers = { 8, 3, 6, 9, 4, 2, 1, 7, 5 };
double result =numbers.Average();
 
VB.NET
 
Dim numbers As Integer() = {8, 3, 6, 9, 4, 2, 1, 7, 5}
Dim result As Double = numbers.Average()
 
The result will be:
5
 
“Average” method can be used to calculate Average length of strings.
 
Example:
 
C#
 
string[] strings = { "USA", "UK", "Canada", "France" };
double result = strings.Average(x => x.Length);
 
VB.NET
 
Dim strings As String() = {"USA", "UK", "Canada", "France"}
Dim result As Double = strings.Average(Function(x) x.Length)
 
The result will be:
4.25
 
Count
 
“Count” method is used to count the elements in the collection.
 
Example:
 
C#
 
int[] numbers = { 8, 3, 6, 9, 4, 2, 1, 7, 5 };
int result = numbers.Count();
 
VB.NET
 
Dim numbers As Integer() = {8, 3, 6, 9, 4, 2, 1, 7, 5}
Dim result As Integer = numbers.Count()
 
The result will be:
9
 
Min and Max
 
“Min” method finds the minimum element from the collection.
 
Example:
 
C#
 
int[] numbers = { 8, 3, 6, 9, 4, 2, 1, 7, 5 };
int result = numbers.Min();
 
VB.NET
 
Dim numbers As Integer() = {8, 3, 6, 9, 4, 2, 1, 7, 5}
Dim result As Integer = numbers.Min()
 
The result will be:
1
 
“Max” method finds the maximum element from the collection.
 
Example:
 
C#
 
int[] numbers = { 8, 3, 6, 9, 4, 2, 1, 7, 5 };
int result2 = numbers.Max();
 
VB.NET
 
Dim numbers As Integer() = {8, 3, 6, 9, 4, 2, 1, 7, 5}
Dim result2 As Integer = numbers.Max()
 
The result will be:
9
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to insert record into SQL Server database using Stored Procedure in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net