👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Using LINQ Join to combine data

Using LINQ Join to combine data

Author - Abdul Rahman (Bhai)

LINQ

26 Articles

Improve

Table of Contents

  1. What we gonna do?
  2. Why we gonna do?
  3. How we gonna do?
  4. Summary

What we gonna do?

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

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

When working with two collections, we can combine them using LINQ Join() method. This will join elements between two or more collections and gives a single collection. Join() also know as Equi Join or Inner Join in SQL. To work with Join(), we need atleast one property in each collection to share equal value.

Why we gonna do?

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

  • Joining collections using common key value to define relationship between them
  • Joining Customer and their Orders
  • Joining Order and Order Items

How we gonna do?

Using LINQ Join to combine primitive types

Primitive data types like int, decimal, string, etc can just combine the values against other equal values in the collection


List<int> list = new() { 1, 2, 3, 4, 5 };
List<int> anotherList = new() { 4, 5, 6 };

//Method Syntax
var result = list
             .Join(anotherList, 
                   number => number, 
                   anotherNumber => anotherNumber, 
                   (number, anotherNumber) => new { number, anotherNumber ])
             .ToList();
                    
//Query Syntax
var result = (from number in list
             join anotherNumber in anotherList 
             on number equals anotherNumber
             select new { number, anotherNumber })
             .ToList();
        
Demo Space

Using LINQ Join to combine Collections using single field

We can use Join() to 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 and their associated values.


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

//Method Syntax
List<ProductSale> result = products
                           .Join(sales, 
                                 product => product.Id,
                                 sale => sale.ProductId,
                                 (product, sale) => new ProductSale
                                 {
                                    ProductId = product.Id,
                                    SaleId = sale.Id,
                                    Name = product.Name,
                                    Price = sale.Price
                                 })
                           .ToList();
                    
//Query Syntax
List<ProductSale> result = (from product in products
                            join sale in sales 
                            on product.Id equals sale.ProductId
                            select new ProductSale
                             {
                                ProductId = product.Id,
                                SaleId = sale.Id,
                                Name = product.Name,
                                Price = sale.Price
                             }) 
                           .ToList();
        
Demo Space

Using LINQ Join to combine Collections using multiple fields

So for joining collections with more than one field, the key selector condition will have an anonymous object containing multiple fields to compare.


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

//Method Syntax
List<ProductSale> result = products
                           .Join(sales, 
                                 product => new { ProductId = product.Id, ProductColor = product.Color },
                                 sale => new { ProductId = sale.ProductId, ProductColor = sale.ProductColor },
                                 (product, sale) => new ProductSale
                                 {
                                    ProductId = product.Id,
                                    SaleId = sale.Id,
                                    Name = product.Name,
                                    Price = sale.Price,
                                    Color = product.Color
                                 })
                           .ToList();
                    
//Query Syntax
List<ProductSale> result = (from product in products
                            join sale in sales 
                            on 
                              new { ProductId = product.Id, ProductColor = product.Color } 
                            equals 
                              new { ProductId = sale.ProductId, ProductColor = sale.ProductColor }
                            select new ProductSale
                             {
                                ProductId = product.Id,
                                SaleId = sale.Id,
                                Name = product.Name,
                                Price = sale.Price,
                                Color = product.Color
                             }) 
                           .ToList();
        
Demo Space

Summary

In this article we learn't how to combine data between collections using Join. This can be used to combine items between collection and return a new collection. This can be done using the key selector. We can have single key selector or multiple key selector as anonymous object. All these can be used with any IEnumerable or IQueryable types.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • LINQ
  • Join
  • Combine
  • Equi Join
  • Inner Join