C# > Advanced C# > LINQ > Joining Collections
LINQ Join with Anonymous Types
This code snippet demonstrates how to use LINQ's `Join` method to combine two collections based on a common property. It utilizes anonymous types for creating the result, showcasing a concise way to project the joined data into a new format without defining a dedicated class.
Code Snippet
The code first defines two classes, `Product` and `Order`, representing entities with a common `ProductId`. Two lists, `products` and `orders`, are created and populated with sample data. The `Join` method is then used to combine these lists based on the `ProductId`. The lambda expressions `product => product.ProductId` and `order => order.ProductId` specify the keys to join on. Finally, an anonymous type is used to create a new object containing the `ProductName` from the `Product` object, and the `OrderId` and `Quantity` from the `Order` object. The joined data is then iterated through, and the results are printed to the console.
using System;
using System.Collections.Generic;
using System.Linq;
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
}
public class Order
{
public int OrderId { get; set; }
public int ProductId { get; set; }
public int Quantity { get; set; }
}
public class Example
{
public static void Main(string[] args)
{
List<Product> products = new List<Product>
{
new Product { ProductId = 1, ProductName = "Laptop" },
new Product { ProductId = 2, ProductName = "Mouse" },
new Product { ProductId = 3, ProductName = "Keyboard" }
};
List<Order> orders = new List<Order>
{
new Order { OrderId = 101, ProductId = 1, Quantity = 2 },
new Order { OrderId = 102, ProductId = 2, Quantity = 5 },
new Order { OrderId = 103, ProductId = 1, Quantity = 1 }
};
var joinedData = products.Join(
orders,
product => product.ProductId,
order => order.ProductId,
(product, order) => new
{
ProductName = product.ProductName,
OrderId = order.OrderId,
Quantity = order.Quantity
}).ToList();
foreach (var item in joinedData)
{
Console.WriteLine($"Product: {item.ProductName}, Order ID: {item.OrderId}, Quantity: {item.Quantity}");
}
}
}
Concepts Behind the Snippet
This snippet leverages LINQ's join capabilities, which provide a declarative way to combine data from multiple collections. The `Join` method performs an inner join, meaning it only returns elements where a matching key exists in both collections. Anonymous types allow for creating lightweight objects without needing to explicitly define a class, which is useful for projection scenarios like this.
Real-Life Use Case
Consider an e-commerce application. You might have a `Customers` table and an `Orders` table. You could use LINQ's `Join` method to retrieve all orders placed by a specific customer, combining data from both tables based on the `CustomerId`.
Best Practices
Interview Tip
Be prepared to explain the different types of joins available in LINQ (inner join, left join, right join, full outer join). Understand the performance implications of joining large datasets.
When to Use Them
Use LINQ's `Join` method when you need to combine data from two or more collections based on a common property. It's particularly useful when working with data from databases or other external sources.
Memory Footprint
The memory footprint depends on the size of the collections being joined and the number of matching elements. Large datasets can consume significant memory. Consider using deferred execution and streaming approaches for very large datasets to minimize memory usage.
Alternatives
Pros
Cons
FAQ
-
What is the difference between `Join` and `GroupJoin`?
`Join` performs an inner join, returning only elements where a matching key exists in both collections. `GroupJoin` performs a left outer join, returning all elements from the first collection along with a collection of matching elements from the second collection. -
Can I join more than two collections using LINQ?
Yes, you can chain multiple `Join` operations together to join more than two collections. However, this can become complex and may impact performance.