In programming, you often want to treat data managed as separate arrays or lists as a single, unified list.
The Concat method included in C# LINQ allows you to connect another collection directly to the end of an existing one. In this article, I will explain this using a scenario of merging “morning orders” and “afternoon orders” in an e-commerce system to create a daily order list.
Overview of the Concat Method
The Concat method concatenates two sequences (arrays, Lists, etc.) and returns a new sequence. Its behavior is very simple: it adds the elements of the “collection specified in the argument” directly after the “caller collection.”
Difference from the Union Method
There is a similar method called Union, but there is a clear difference between them.
- Union: Removes duplicate elements (Set union).
- Concat: Keeps duplicate elements (Simple concatenation).
Use Concat when you want to maintain data order or process all data including duplicates.
Practical Code Example: Integrating Daily Order Data
The following code merges order lists separated by time of day to create a summary list for reporting.
using System;
using System.Collections.Generic;
using System.Linq;
namespace OrderProcessingSystem
{
// Order data
public record Order(int Id, string ItemName, int Quantity);
class Program
{
static void Main()
{
// Scenario:
// Due to system design, order data is saved separately as "morning" and "afternoon".
// We want to simply combine these to create a full order list for the day.
// * Since the same item might be ordered multiple times, we do not remove duplicates.
// Morning order list
var morningOrders = new[]
{
new Order(101, "Office Chair", 1),
new Order(102, "USB-C Cable", 3),
new Order(103, "Monitor Arm", 1)
};
// Afternoon order list
var afternoonOrders = new[]
{
new Order(104, "Wireless Mouse", 5),
new Order(105, "USB-C Cable", 2), // Contains the same item as morning
new Order(106, "Laptop Stand", 1)
};
// Concatenate lists using Concat
// afternoonOrders follows the end of morningOrders.
IEnumerable<Order> allOrders = morningOrders.Concat(afternoonOrders);
// Output results
Console.WriteLine("--- Today's Full Order List ---");
foreach (var order in allOrders)
{
Console.WriteLine($"ID: {order.Id} | {order.ItemName} (Quantity: {order.Quantity})");
}
// (Reference) Check count
Console.WriteLine($"\nTotal count: {allOrders.Count()} items");
}
}
}
Execution Result
--- Today's Full Order List ---
ID: 101 | Office Chair (Quantity: 1)
ID: 102 | USB-C Cable (Quantity: 3)
ID: 103 | Monitor Arm (Quantity: 1)
ID: 104 | Wireless Mouse (Quantity: 5)
ID: 105 | USB-C Cable (Quantity: 2)
ID: 106 | Laptop Stand (Quantity: 1)
Total count: 6 items
Technical Points and Precautions
1. Deferred Execution
The Concat method uses deferred execution. Heavy processes like memory copying do not occur when the method is called. Access happens sequentially only when results are enumerated using foreach or similar methods. Therefore, it works with high memory efficiency even when logically combining very large log files and reading them line by line.
2. Adding a Single Element
If you want to add just one element to the end of a list instead of combining collections, the Append method (available in .NET Framework 4.7.1 / .NET Core 1.0 and later) is convenient. Conversely, use Prepend to add to the beginning.
// Add a single order to the end of the list
var newSequence = allOrders.Append(new Order(107, "Additional Order", 1));
3. Handling Null
If the argument of Concat (the collection to be connected) is null, an ArgumentNullException occurs. If the connection target might be empty, you need to check it beforehand or ensure you use an empty collection (Enumerable.Empty<T>()).
Summary
The Concat method is the most basic way to join multiple data sources in sequence. Choose Concat instead of Union for requirements where you want to “process all data in order without modification or removing duplicates.” Since it is simple and fast, it is ideal for integrating logs and aggregating data in batch processing.
