Inner Join in C# LINQ
By FoxLearn 1/20/2025 3:14:00 AM 97
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
andCategories
, 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
andcategories
, are created with a shared key,CategoryId
. - Using the Join Clause: The
join
keyword combines products with their respective categories based on the matchingCategoryId
. - 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.
- How to get the index of an element in C# LINQ
- Cannot use a lambda expression as an argument to a dynamically dispatched operation
- How to group by multiple columns using LINQ
- Using LINQ to remove elements from a List<T>
- How to Find XML element by name with XElement in LINQ
- Could not find an implementation of the query pattern for source type
- Filtering Collections in LINQ
- Element Operators in LINQ
Categories
Popular Posts
Freedash bootstrap lite
11/13/2024
How to secure ASP.NET Core with NWebSec
11/07/2024
Spica Admin Dashboard Template
11/18/2024