Posted by: Cirilo Meggiolaro | 01/13/2009

Tip of the day #91 – LongCount method – Enumerable class

The LongCount method available from any class that implements the IEnumerable is used to count the number of occurrences for a given object or expression and retrieve an int64 number that represents the result of the count.

Method Signatures

There are two method overloads available:

  • The first one returns an int64 that represents the total number of items in an IEnumerable object;

public static long LongCount<TSource>(this IEnumerable<TSource> source);

  • The second overloads returns an int64 based on a given expression.

public static long LongCount<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

Example

For this example we are going to use the LongCount method to count customers that have bought just once, that have between 2 and 10 orders and more than 10. We are going to use the following class as our Customer class:

public class Customer
{
    public Customer(int id, string name, int numberOfOrders)
    {
        this.ID = id;
        this.Name = name;
        this.NumberOfOrders = numberOfOrders;
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public int NumberOfOrders { get; set; }
}

Customer class created, it’s time to create a generic list of customers:

List<Customer> customers = new List<Customer>()
{
    new Customer(1, “George Smith”, 1),
    new Customer(2, “Bill Anderson”, 2),
    new Customer(3, “Michael de Souza”, 4),
    new Customer(4, “Sam Ng”, 12),
    new Customer(5, “Jimmy Lee”, 6)
};

And here is the easiest part. We are going to use a lambda expression to create a condition based on the NumberOfOrders property from our customer class and write the totals to the console. If you haven’t read anything about lambda expressions yet, take a break to read the Tip #25 – Lambda Expressions.

The final code must be similar to the following:

Console.WriteLine(“Customers that have bought just once: {0}”, customers.LongCount(c => c.NumberOfOrders == 1));

Console.WriteLine(“Customers that have bought more than once but no more than 10 times: {0}”, customers.LongCount(c => c.NumberOfOrders > 1 && c.NumberOfOrders <= 10));

Console.WriteLine(“Customers that have bought more than 10 times: {0}”, customers.LongCount(c => c.NumberOfOrders > 10));

Output

Customers that have bought just once: 1
Customers that have bought more than once but no more than 10 times: 3
Customers that have bought more than 10 times: 1

Advertisements

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: