Posted by: Cirilo Meggiolaro | 02/27/2009

Tip of the day #136 – Average method – Enumerable class

The Average extension method provides an easy way to calculate an average value based on either a numeric IEnumerable source or a custom IEnumerable source and a selection function. Several overloads are available as described below:

public static decimal? Average(this IEnumerable<decimal?> source);
public static decimal Average(this IEnumerable<decimal> source);
public static double? Average(this IEnumerable<double?> source);
public static double Average(this IEnumerable<double> source);
public static float? Average(this IEnumerable<float?> source);
public static float Average(this IEnumerable<float> source);
public static double? Average(this IEnumerable<int?> source);
public static double Average(this IEnumerable<int> source);
public static double? Average(this IEnumerable<long?> source);
public static double Average(this IEnumerable<long> source);
public static decimal? Average<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector);
public static decimal Average<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector);
public static double? Average<TSource>(this IEnumerable<TSource> source, Func<TSource, double?> selector);
public static double Average<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector);
public static float? Average<TSource>(this IEnumerable<TSource> source, Func<TSource, float?> selector);
public static float Average<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector);
public static double? Average<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector);
public static double Average<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector);
public static double? Average<TSource>(this IEnumerable<TSource> source, Func<TSource, long?> selector);
public static double Average<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector);

How to…

Just to start let’s create a simple array of decimals that represents scores for a student and calculate the average score for the year:

decimal[] biMonthlyScore = { 7, 8.5M, 9, 9.5M };
decimal averageScore = biMonthlyScore.Average();
Output: 8.5

Let’s code now a more complex example. Given a Customer class that contains a list of Orders objects we are going to calculate the average of orders by customer and the average of billed value by client. For the second example we are going to use the Average method in conjunction with the Sum method.

public class Customer
{
    public int ID { get; set; }
public string Name { get; set; }
    public DateTime DOB { get; set; }
    public List<Order> Orders { get; set; }
}

public class Order
{
    public int ID { get; set; }
    public decimal BilledTotal { get; set; }
    public DateTime Date { get; set; }
}

Given a generic list of customers let’s implement now the code to calculate:

  • The average of orders by customer: The code checks if the customer has a valid instance of the orders collection:

double averageOfOrders = _customers.Average(c => c.Orders != null ? c.Orders.Count : 0);

  • An average of billed value by client: The following code performs two tasks. The first one gets the billed total by client using the Sum method. The second task calculates the average between all clients:

double billedAverage = _customers.Average(c => c.Orders != null ? c.Orders.Sum(o => (double)o.BilledTotal) : 0);


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 )

Google+ photo

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

Connecting to %s

Categories

%d bloggers like this: