Inner Join in C# LINQ

By FoxLearn 1/20/2025 3:14:00 AM   15
An inner join in C# LINQ allows you to create a result set by combining elements from two collections where a matching key exists.

If an element in the first collection has no matching key in the second collection, it is excluded from the result set.

Use Cases of Inner Join in LINQ

  • Combine related data from two collections based on a shared attribute.
  • Filter out elements that don’t match the other collection.
  • Project the combined data into a new structure or format.

Steps to Perform Inner Join

  • Define the Data Sources: Prepare two collections, such as Products and Categories, with a common key.
  • Use the Join Clause: Perform the join based on the shared key (e.g., CategoryId).
  • Project the Results: Use the select keyword to shape the output into the desired form.

For example, Inner Join with Products and Categories

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        // Data sources
        var products = new List<Product>
        {
            new Product { Id = 1, Name = "Laptop", CategoryId = 1 },
            new Product { Id = 2, Name = "Phone", CategoryId = 1 },
            new Product { Id = 3, Name = "Desk", CategoryId = 2 },
            new Product { Id = 4, Name = "Chair", CategoryId = 2 },
            new Product { Id = 5, Name = "Headphones", CategoryId = null }
        };

        var categories = new List<Category>
        {
            new Category { Id = 1, Name = "Electronics" },
            new Category { Id = 2, Name = "Furniture" }
        };

        // Perform inner join
        var query = from p in products
                    join c in categories
                    on p.CategoryId equals c.Id
                    select new
                    {
                        ProductName = p.Name,
                        CategoryName = c.Name
                    };

        // Display results
        foreach (var result in query)
        {
            Console.WriteLine($"{result.ProductName} belongs to {result.CategoryName}");
        }
    }
}

// Product class
class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? CategoryId { get; set; }
}

// Category class
class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
}

In this example:

  • Defining Data Sources: Two lists, products and categories, are created with a shared key, CategoryId.
  • Using the Join Clause: The join keyword combines products with their respective categories based on the matching CategoryId.
  • Projecting Results: The query projects the product and category names into a new format.

Output:

Laptop belongs to Electronics
Phone belongs to Electronics
Desk belongs to Furniture
Chair belongs to Furniture

This example demonstrates how to efficiently combine and filter data using an inner join in LINQ. Products without a category (CategoryId = null) are excluded from the output.