BlackWaspTM
LINQ
.NET 3.5+

LINQ Grouped Joins

The ninth part of the LINQ to Objects tutorial looks at grouped joins. These allow two collections to be combined in a join operation based upon matching key values. The results are then grouped into keyed collections that may be aggregated.

Grouped Joins

In earlier articles in this series we have examined grouping and joining operations. Grouping in LINQ breaks a set of data into a number of smaller sets, each sharing a key value. Joining allows the information from two or more collections to be combined into a single list. In this article we will look at grouped joins. A grouped join provides similar functionality to grouping and joining. An outer list and an inner list are joined into a single entity and then grouped so that each outer element is paired with the list of matching inner items.

To demonstrate grouped joins we need some sample classes and data. These are the same as those in the previous article. One class represents stock items and a second describes the categories that may be applied to those items.

First, add the StockItem class to a new Console Application project:

public class StockItem
{
    public string Name { get; set; }
    public string Category { get; set; }
    public double Price { get; set; }

    public StockItem(string name, string category, double price)
    {
        Name = name;
        Category = category;
        Price = price;
    }
}

You can now add the StockCategory class:

public class StockCategory
{
    public string Name { get; set; }
    public string MajorCategory { get; set; }

    public StockCategory(string name, string majorCategory)
    {
        Name = name;
        MajorCategory = majorCategory;
    }
}

To create an example list of stock items, add the following code:

var stock = new List<StockItem>
{
    new StockItem("Apple", "Fruit", 0.30),
    new StockItem("Banana", "Fruit", 0.35),
    new StockItem("Orange", "Fruit", 0.29),
    new StockItem("Cabbage", "Vegetable", 0.49),
    new StockItem("Carrot", "Vegetable", 0.29),
    new StockItem("Lettuce", "Vegetable", 0.30),
    new StockItem("Milk", "Dairy", 1.12)
};

To complete the test data add the following sample category data:

var categories = new List<StockCategory>
{
    new StockCategory("Dairy", "Chilled"),
    new StockCategory("Fruit", "Fresh"),
    new StockCategory("Vegetable", "Fresh")
};
2 September 2010