Posted by: Cirilo Meggiolaro | 02/4/2009

Tip of the day #113 – IGrouping Interface

Let’s check today how to work with the generic IGrouping interface that allows you to group a set of common objects within a collection that have the same key and loop through it using for each statement.

Interface definition

The generic IGrouping interface accepts a key data type that is common for all objects within the group and the group elements data type as shown on the definition below:

public interface IGrouping<TKey, TElement> : IEnumerable<TElement>, IEnumerable
    TKey Key { get; }

How to…

In our example we are going to create a list of Orders and group them based on the customer id. Before we start, let’s assume that we have the following Order class:

public class Order
    public int OrderID { get; set; }
    public int CustomerID { get; set; }
    public DateTime OrderDate { get; set; }
    public decimal BilledTotal { get; set; }

The first step is to have a collection of Order objects:

Order[] orders = new Order[6]
    new Order() { OrderID = 1, CustomerID = 1, OrderDate = DateTime.Now, BilledTotal = 1234.56M },
    new Order() { OrderID = 2, CustomerID = 2, OrderDate = DateTime.Now, BilledTotal = 4.99M },
    new Order() { OrderID = 3, CustomerID = 2, OrderDate = DateTime.Now, BilledTotal = 999.00M },
    new Order() { OrderID = 4, CustomerID = 1, OrderDate = DateTime.Now, BilledTotal = 78.99M },
    new Order() { OrderID = 5, CustomerID = 1, OrderDate = DateTime.Now, BilledTotal = 8.99M },
    new Order() { OrderID = 6, CustomerID = 3, OrderDate = DateTime.Now, BilledTotal = 15.38M }

The next step is to declare the IGrouping interface, defining the key as an integer, because we are going to group by the customer id that is an integer property and the element data type as an Order because these are the objects we want to group.

IGrouping<int, Order> customerOrders = orders.GroupBy(o => o.CustomerID).First();

You may have noticed that the code above calls the First<> extension method. The intention was to group all the orders based on the customer id from the first Order within the collection. If you want to specify a customer id your code would be similar to the following:

IGrouping<int, Order> customerOrders = orders.GroupBy(o => o.CustomerID).Where(o => o.Key == 2).First();

To retrieve the key for the current group you may use the Key property:

Console.WriteLine(“Customer ID: {0}”, customerOrders.Key);

Once you have the group you may loop through it using a for each statement.

foreach (Order order in customerOrders)
    Console.WriteLine(“Order ID: {0}, Order Date: {1}, Total Value: {2}”, order.OrderID, order.OrderDate, order.BilledTotal);

Leave a Reply

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

You are commenting using your 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


%d bloggers like this: