👉🏼 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

linq

26 Articles

Improve

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.

Table of Contents

  1. Introduction
  2. Using LINQ Join to combine primitive types
  3. Using LINQ Join to combine collections using single field
  4. Using LINQ Join to combine collections using multiple fields
  5. Summary

Introduction

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.

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

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

Code Sample - LINQ Join Primitive Types

Demo - LINQ Join Clause Demo

Let's try LINQ Join with integer type

  1. Enter number and add it to respective sequence
  2. For demo purpose I have restricted sequence length to 3
  3. Click on Join Button to view the result
  4. Click on reset to try other combination

Joined sequence

Number Another Number

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.

Code Sample - LINQ Join Objects with Single Field

Demo - LINQ Join Clause with Objects Demo

Let's try LINQ Join with Objects

  • We have Product class with following properties - Id, Name
  • We have Sale class with following properties - Id, Price
  • The tables are loaded with product and sale collection.
  • The Key Expression here is the Product ID. Join() will combine Products and Sales and return new ProductSale collection.
  • Click on Join Button to view the result.
  • Click on reset to try other combination
Products
Id Name
1 Shirt
2 Shirt
3 Shirt
4 Shirt
5 Shirt
6 Shirt
Sales
Sale Id Product Id Price
123 1 1000
789 1 1000
456 2 1000
Sale Id Product Id Name Price

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.

Code Sample - LINQ Join Objects with Multiple Fields

Demo - LINQ Join Clause with Objects Demo

Let's try LINQ Join with Objects

  • We have Product class with following properties - Id, Name
  • We have Sale class with following properties - Id, Price
  • The tables are loaded with product and sale collection.
  • The Key Expression here is the Product ID and Product Color. Join() will combine Products and Sales and return new ProductSale collection.
  • Click on Join Button to view the result.
  • Click on reset to try other combination
Products
Id Name Color
1 Shirt Black
2 Shirt Red
3 Shirt Black
4 Shirt Red
5 Shirt Brown
6 Shirt White
Sales
Sale Id Product Id Product Color
123 1 Black
789 1 Red
456 2 White
Sale Id Product Id Name Color

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