LINQ Zip
Zip is a LINQ method used to combine two sequences together, item by item. It takes the first item from the first collection, combines it with the first item from the second collection, then does the same for the second, third, fourth items, and so on.
Basic Example
var names = new[] { "Dave", "Tracey", "Bob" };
var ages = new[] { 59, 58, 42 };
var result = names.Zip(ages, (name, age) => $"{name} is {age}");
foreach (var item in result)
{
Console.WriteLine(item);
}
This produces:
Dave is 59
Tracey is 58
Bob is 42
How Zip Thinks About the Data
names: Dave Tracey Bob
ages: 59 58 42
Zip: Dave+59 Tracey+58 Bob+42
The important point is that Zip matches items by their position in the collection.
It does not match by ID, name, key, or value.
Returning Objects
Instead of returning strings, you can use Zip to create objects.
var names = new[] { "Dave", "Tracey", "Bob" };
var ages = new[] { 59, 58, 42 };
var people = names.Zip(ages, (name, age) => new
{
Name = name,
Age = age
});
This creates a new sequence containing objects with a Name and an Age.
What Happens If the Lists Are Different Lengths?
var letters = new[] { "A", "B", "C", "D" };
var numbers = new[] { 1, 2 };
var result = letters.Zip(numbers, (letter, number) => $"{letter}{number}");
The result is:
A1
B2
Zip stops as soon as the shortest collection runs out of items.
In this example, C and D are ignored.
Zip vs Select
| Method | Purpose |
|---|---|
Select |
Transforms each item in one collection. |
Zip |
Combines matching-position items from two collections. |
Use Select when working with one sequence.
Use Zip when working with two sequences side by side.
// Select
var doubled = numbers.Select(x => x * 2);
// Zip
var combined = names.Zip(ages, (name, age) => $"{name} is {age}");
When Would You Use Zip?
- Combining names and values.
- Pairing old values with new values.
- Combining X and Y coordinates.
- Comparing two lists item by item.
- Creating display text from two related sequences.
Example: Comparing Old and New Values
var oldPrices = new[] { 10.00m, 20.00m, 30.00m };
var newPrices = new[] { 12.00m, 18.00m, 35.00m };
var changes = oldPrices.Zip(newPrices, (oldPrice, newPrice) => new
{
OldPrice = oldPrice,
NewPrice = newPrice,
Difference = newPrice - oldPrice
});
This is a good example of where Zip makes the code clean.
Each old price is paired with the new price in the same position.
Zip Is Not Join
Zip does not look for matching keys.
It only works by position.
If you need to match customers to orders by CustomerId, use Join.
If you need to combine item 1 with item 1, item 2 with item 2, and item 3 with item 3,
use Zip.
Summary
Zipcombines two sequences.- It works item by item, based on position.
- It stops when the shortest sequence ends.
- It is useful for comparing or combining related lists.
- Use
Selectfor one list,Zipfor two lists, andJoinfor key-based matching.

















