Keywords: C# | LINQ | List Joining
Abstract: This article delves into how to join two lists in C# using LINQ query syntax and Lambda expressions, with examples based on WorkOrder and PlannedWork classes. It explains the core mechanisms of Join operations, performance considerations, and practical applications, helping developers enhance data processing efficiency and code maintainability.
In C# programming, joining multiple lists based on specific keys is a common task for consolidating related information. LINQ (Language Integrated Query), as a powerful feature of the .NET framework, offers two primary approaches: query syntax and Lambda expressions. This article uses the WorkOrder and PlannedWork classes as examples to systematically explain how to efficiently join lists with these technologies, analyzing their underlying principles and best practices.
Core Concepts and Data Models
Assume we have two lists: List<WorkOrder> and List<PlannedWork>. The WorkOrder class includes WorkOrderNumber and WorkDescription properties for storing basic work order information, while the PlannedWork class contains WorkOrderNumber and ScheduledDate properties to record planned work schedules. The goal is to join these lists by WorkOrderNumber to retrieve each work order's description and its scheduled date.
Implementing Joins with LINQ Query Syntax
LINQ query syntax provides a declarative, SQL-like writing style that enhances code readability. Based on the best answer, the join operation can be implemented as follows:
var query = from order in workOrders
join plan in plans
on order.WorkOrderNumber equals plan.WorkOrderNumber
select new
{
order.WorkOrderNumber,
order.Description,
plan.ScheduledDate
};
This code starts from the workOrders list, uses the join keyword to connect with the plans list, with the condition that WorkOrderNumber values are equal. The select clause creates an anonymous type containing the desired properties. This method is advantageous for its intuitiveness, especially suitable for complex queries or beginners.
Using Lambda Expressions and Extension Methods
As a supplement, Lambda expressions offer a functional programming style. Using the Join extension method, the code is:
var results = workOrders.Join(plans,
wo => wo.WorkOrderNumber,
p => p.WorkOrderNumber,
(order,plan) => new {order.WorkOrderNumber, order.WorkDescription, plan.ScheduledDate}
);
Here, the Join method takes four parameters: the target list to join, the key selector for the first list, the key selector for the second list, and the result selector. The Lambda expression wo => wo.WorkOrderNumber defines the key extraction logic. This approach is more concise and suitable for scenarios preferring method chaining.
Performance and Applicability Analysis
In terms of performance, both methods rely on hash join algorithms at their core, with approximate O(n) time complexity, ensuring high efficiency. Query syntax is compiled into equivalent Lambda expressions, so there is no significant runtime performance difference. The choice depends on coding style: query syntax is more readable for complex multi-table joins, while Lambda expressions are more flexible for integration into method chains.
Practical Applications and Extensions
In real-world projects, join operations are commonly used in data reporting, API response consolidation, and similar scenarios. For example, in manufacturing systems, combining work order descriptions with scheduled dates can generate production schedules. Developers should handle null values or duplicate keys by using DefaultIfEmpty for left joins or Distinct to avoid data redundancy.
In summary, mastering LINQ join techniques not only improves code efficiency but also enhances data processing capabilities. It is recommended to deepen understanding through practice, such as joining more lists or adding filtering conditions to address more complex business needs.