The Aggregate operators (p1)

            1. What are the Count, LongCount operators?

            2. What are the signatures of the Count, LongCount operators?

            3. What is the Sum operator?

            4. What are the signatures of the Sum operator?

            5. What are the Min, Max operators?

            6. What are the signatures of the Min, Max operators?

            7. What is the Average operator?

            8. What are the signatures of the Average operator?

            9. What is the Aggregate operator?

            10. What are the signatures of the Aggregate operator?

            11. When do we need the Aggregate operator?

            The Count operator counts the number of elements in a sequence.

There are two versions of the Count operator:

 

public static int Count<TSource>(

    this IEnumerable<TSource> source);

 

public static int Count<TSource>(

    this IEnumerable<TSource> source,

    Func<TSource, bool> predicate);

 

public static long LongCount<TSource>(

    this IEnumerable<TSource> source);

public static long LongCount<TSource>(

    this IEnumerable<TSource> source,

    Func<TSource, bool> predicate);

 

Example 1:

 

            var numOrdersByCustomer = from c in Data.Customers                                     

                                      select new { c.Name, NumOrders = c.Orders.Count()};

Output:

 

{ Name = Paolo, NumOrders = 2 }

{ Name = Marco, NumOrders = 2 }

{ Name = James, NumOrders = 1 }

{ Name = Frank, NumOrders = 1 }

 

            The Sum operator computes the sum of a sequence of numeric values.

public static Numeric Sum(

    this IEnumerable<Numeric> source);

 

public static Numeric Sum<TSource>(

    this IEnumerable<TSource> source,

    Func<TSource, Numeric> selector);

 

Example 2:

 

Trace.WriteLine(new []{1, 2, 3}.Sum());

 

Output:

6

 

Example 3:

 

            var amountByCustomer = 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 })

                                        on p.IdProduct equals order.IdProduct

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

                                   on c.Name equals data.Name into result

                                   select new { c.Name, TotalAmount = result.Sum(o => o.Amount)};

Output:

Name: Paolo – TotalAmount: 130

Name: Marco – TotalAmount: 700

Name: James – TotalAmount: 600

Name: Frank – TotalAmount: 1000

 

The Min,Max operators finds the minimum, maximum of a sequence of numeric values.

public static Numeric Min(

    this IEnumerable<Numeric> source);

public static TSource Min<TSource>(

    this IEnumerable<TSource> source);

public static Numeric Min<TSource>(

    this IEnumerable<TSource> source,

    Func<TSource, Numeric> selector);

public static TResult Min<TSource, TResult>(

    this IEnumerable<TSource> source,

    Func<TSource, TResult> selector);

 

public static Numeric Max(

    this IEnumerable<Numeric> source);

public static TSource Max<TSource>(

    this IEnumerable<TSource> source);

public static Numeric Max<TSource>(

    this IEnumerable<TSource> source,

    Func<TSource, Numeric> selector);

public static TResult Max<TSource, TResult>(

    this IEnumerable<TSource> source,

    Func<TSource, TResult> selector);

 

Example 4:

 

            var extremeCustomers = from data in amountByCustomer

                                   where (data.TotalAmount == amountByCustomer.Min(o => o.TotalAmount) ||

                                           data.TotalAmount == amountByCustomer.Max(o => o.TotalAmount))

                                   select data.Name;

Output:

Paolo

Frank

 

The Average operator computes the average of a sequence of numeric values.

public static Result Average(   

    this IEnumerable<Numeric> source);

public static Result Average<TSource>(

    this IEnumerable<TSource> source,

    Func<TSource, Numeric> selector);

 

Example 5:

 

            var averageAmountByCustomer = 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})

                                                            on p.IdProduct equals order.IdProduct

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

                                          on c.Name equals data.Name into cdata

                                          select new {c.Name, AverageAmount = cdata.Average( o => o.Amount) };

 

Output:

Name: Paolo; Average: 65

Name: Marco; Average: 350

Name: James; Average: 600

Name: Frank; Average: 1000

 

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