The Aggregate operators (p2)

When we don’t have special aggregate function or we need to get a special result we can use the Aggregate operator which applies a function over a sequence.

public static TSource Aggregate<TSource>(

    this IEnumerable<TSource> source,

    Func<TSource, TSource, TSource> func);

public static TAccumulate Aggregate<TSource, TAccumulate>(

    this IEnumerable<TSource> source,

    TAccumulate seed,

    Func<TAccumulate, TSource, TAccumulate> func);

public static TResult Aggregate<TSource, TAccumulate, TResult>(

    this IEnumerable<TSource> source,

    TAccumulate seed,

    Func<TAccumulate, TSource, TAccumulate> func,

    Func<TAccumulate, TResult> resultSelector);

 

The Aggregate operators with a seed value start by assigning the seed value to an internal accumulator. They then enumerate the source sequence, repeatedly computing the next accumulator value by invoking the specified function with the current accumulator value as the first argument and the current sequence element as the second argument. The operator without a result selector returns the final accumulator value as the result. The operator with a result selector passes the final accumulator value to the supplied result selector and returns the resulting value. An ArgumentNullException is thrown if the source, func or resultSelector argument is null.

The Aggregate operator without a seed value uses the first element of the source sequence as the seed value, but otherwise functions as described above. If the source sequence is empty, the Aggregate operator without a seed value throws an InvalidOperationException.

 

 

Example 6:

 

            var theBestProduct = from p in Data.Products

                                 join order in

                                     (from c in Data.Customers

                                      from o in c.Orders

                                      select new { c.Name, o.IdProduct, o.Quantity })

on p.IdProduct equals order.IdProduct into porder

                                 select new { p.IdProduct,

Sum = porder.Sum(o => p.Price * o.Quantity) };

 

            var theBestProduct = from p in Data.Products

                                 join order in

                                     (from c in Data.Customers

                                      from o in c.Orders

                                      select new { c.Name, o.IdProduct, o.Quantity })

on p.IdProduct equals order.IdProduct into porder

                                 select new { p.IdProduct,

Sum = porder.Aggregate(0m, (accum, order) => accum += order.Quantity * p.Price) };

 

 

Example 7:

 

            var maxAmountMonthByCustomer = from c in Data.Customers

                                              join data in

                                                  (from p in Data.Products

                                                   join order in

                                                       (from c in Data.Customers

                                                        from o in c.Orders

                                                        select new { c.Name, o.IdProduct, o.Quantity, o.Month })

                                                   on p.IdProduct equals order.IdProduct

                                                   select new { order.Name, Amount = order.Quantity * p.Price, order.Month})

                                                on c.Name equals data.Name into cdata

                                            select new { c.Name,

                                                MaxAmount = cdata.Aggregate( new {Ammount = 0m, Month = String.Empty},

                                                    (accum, order) => accum.Ammount > order.Amount ?

                                        accum : new {Ammount = order.Amount, Month = order.Month})};

Output:

 

Name: Paolo; MaxAmount: { Ammount = 100, Month = May }

Name: Marco; MaxAmount: { Ammount = 600, Month = December }

Name: James; MaxAmount: { Ammount = 600, Month = December }

Name: Frank; MaxAmount: { Ammount = 1000, Month = July }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s