LINQ channel icon LINQ · 26 published articles

Using LINQ Group Join to combine data

In this post I will teach you how to combine data between collections using LINQ in .NET. All with live working demo.

Using LINQ Group Join to combine data
On this page

What you'll learn

In this article, let's learn about how to use GroupJoin in LINQ in .NET.

Note: If you have not done so already, I recommend you read the article on Using LINQ Join to combine data.

When working with two collections, we can combine them using LINQ GroupJoin() method. This will join elements between two or more collections and gives a single collection representing one-to-many relationship. GroupJoin() can also be done using Query Synatx using the into keyword similar to SQL. To work with GroupJoin(), we need atleast one property in each collection to share equal value.

Why it matters

LINQ GroupJoin() is used to answer questions about collection such as

  • Joining collections using common key value to define one-to-many relationship between them
  • Grouping Menu and Sub Menu in Navigation bar in UI
  • Grouping Items with parent in Accordion
  • Joining Order and Order Items

Implementation

Using LINQ Group Join to combine collections

GroupJoin() is a very popular thing, a very common thing that you would do in many applications where you have a one‑to‑many relationship. We can combine elements between two or more sequences (arrays, lists, etc.) based on a key value. The result is a new sequence that contains elements with the matching key element and their associated collection values. For Example, new collection of customerOrders for each customer.

C#


List<Product> products = GetProducts();
List<Sale> sales = GetSales();
List<ProductSales> result = new();

//Method Syntax
List<ProductSales> result = products
                           .GroupJoin(sales, 
                                 product => product.Id,
                                 sale => sale.ProductId,
                                 (product, productSales) => new ProductSales
                                 {
                                    Product = product,
                                    Sales = productSales.ToList()
                                 })
                           .ToList();
                    
//Query Syntax
List<ProductSales> result = (from product in products
                            join sale in sales 
                            on product.Id equals sale.ProductId
                            into productSales
                            select new ProductSales
                             {
                                Product = product,
                                Sales = productSales.ToList()
                             }) 
                           .ToList();
        
Live demo: LINQ GroupJoin Clause with Objects Demo — interactive controls are available when the page loads.

Summary

In this article we learn't how to combine data between collections using GroupJoin. This allows you to group elements from one collection with elements from another collection based on a specified key representing one-to-many relationship. All these can be used with any IEnumerable or IQueryable types.