Part 5 Aggregate function in LINQ

preview_player
Показать описание
Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

LINQ Tutorial - All Text Articles & Slides

LINQ Tutorial Playlist

Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses

In this video we will discuss the use of Aggregate() LINQ function. In Part 4 of LINQ Tutorial, we discussed the following functions.
Min
Max
Sum
Count
Average

Let us understand the use of Aggregate() function with examples.

Example 1: Consider the following string array.
string[] countries = { "India", "US", "UK", "Canada", "Australia" };

We want to combine all these strings into a single comma separated string. The output of the program should be as shown below.
India, US, UK, Canada, Australia

Without LINQ, the program will be as shown below.
using System;
namespace Demo
{
class Program
{
static void Main()
{
string[] countries = { "India", "US", "UK", "Canada", "Australia" };

string result = string.Empty;
for (int i = 0; i [ countries.Length ; i++)
{
result = result + countries[i] + ", ";
}

int lastIndex = result.LastIndexOf(",");
result = result.Remove(lastIndex);

Console.WriteLine(result);
}
}
}

With LINQ Aggregate function.
using System;
using System.Linq;
namespace Demo
{
class Program
{
static void Main()
{
string[] countries = { "India", "US", "UK", "Canada", "Australia" };

string result = countries.Aggregate((a, b) =] a + ", " + b);

Console.WriteLine(result);
}
}
}

How Aggregate() function works?
Step 1. First "India" is concatenated with "US" to produce result "India, US"
Step 2. Result in Step 1 is then concatenated with "UK" to produce result "India, US, UK"
Step 3: Result in Step 2 is then concatenated with "Canada" to produce result "India, US, UK, Canada"
This goes on until the last element in the array to produce the final single string "India, US, UK, Canada, Australia"

Example 2: Consider the following integer array
int[] Numbers = { 2, 3, 4, 5 };

Compute the product of all numbers

Without LINQ
using System;
namespace Demo
{
class Program
{
static void Main()
{
int[] Numbers = { 2, 3, 4, 5 };

int result = 1;
foreach (int i in Numbers)
{
result = result * i;
}

Console.WriteLine(result);
}
}
}

With LINQ:
using System;
using System.Linq;
namespace Demo
{
class Program
{
static void Main()
{
int[] Numbers = { 2, 3, 4, 5 };

int result = Numbers.Aggregate((a, b) =] a * b);

Console.WriteLine(result);
}
}
}

How Aggregate() function works?
Step 1. Multiply (2X3) to produce result 6
Step 2. Result (6) in Step 1 is then multiplied with 4 (6X4) to produce result 24
Step 3: Result (24) in Step 2 is then multiplied with 5 (24X5) to produce final result 120

Example 3: Consider the following integer array
int[] Numbers = { 2, 3, 4, 5 };

One of the overloaded version of Aggregate() function has a Seed parameter. If we pass 10 as the value for Seed parameter
int result = Numbers.Aggregate(10, (a, b) =] a * b);

1200 will be the result

Step 1. Multiply (10X2) to produce result 20
Step 2. Result (20) in Step 1 is then multiplied with 3 (20X3) to produce result 60
Step 2. Result (60) in Step 2 is then multiplied with 4 (60X4) to produce result 240
Step 3: Result (240) in Step 3 is then multiplied with 5 (240X5) to produce final result 1200
Рекомендации по теме
Комментарии
Автор

For all of you suggesting using String.Join -> That is not the point of tutorial. It is showing how we can use Aggregate functions and showing Concatenation in loop is easier to understand.

titol
Автор

Hi Venkat! You're the best teacher I have never had !

kamdemkakengne
Автор

The Lectures are really Helpful . Thank's a lot sir.

coderkamboj
Автор

Thank u so much sir.
Please name a good book for LINQ. looking for more videos on linq asap please.

vatansoni
Автор

Hello Sir, Is the memory usage in aggregate functions is mutable or immutable? Please clarify on this .. thanks!

bhavanakandula
Автор

Dear sir, how can we write two or more aggregate function in a LINQ lamda expression/query?

tothedust
Автор

For the exercise the non linq method to add string is better done in loop logic control, IMO. no reapproaching values. After loop declare, result == string.Empty?result = str:result= result + ", " + str; Linq is still easier, but lets look at efficient code.

jasonmorello
Автор

How to get product of a datatable column in linq?

rajeev
Автор

Very nice Sir. May Allah bless you for this humane

muhammadzarshid