Lesson 19/31

Tutorials LINQ Mastery

Zip: Combining two streams

On this page

Combining Sequences

Zip allows you to merge two sequences like a physical zipper, pairing the first elements together, then the second, and so on.

1. Pairing Data

Imagine you have a list of Labels and a list of Values. Zip allows you to create a KeyValuePair or a more complex object from them in a single line.


var keys = new[] { "Name", "Age", "City" };
var values = new[] { "Alice", "30", "London" };
var profile = keys.Zip(values, (k, v) => `${k}: ${v}`);
// Result: { "Name: Alice", "Age: 30", "City: London" }
    

2. Multi-Zip (.NET 6+)

You can now Zip three sequences together: sequenceA.Zip(sequenceB, sequenceC). This returns a sequence of ValueTuples (A, B, C). This is extremely useful for processing parallel data streams from different sources (like CSV columns).

3. Architect Insight

Q: "What if the sequences have different lengths?"

Architect Answer: "Zip stops as soon as the **shortest** sequence ends. If A has 10 items and B has 5, the zipped result will have only 5 items. If you need to keep the extra items, you'll need to use a custom extension method or 'pad' the shorter list with default values beforehand."

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

LINQ Mastery
Course syllabus
General
1. Core Foundations
2. Filtering & Transformation
3. Aggregation & Quantifiers
4. Ordering & Partitioning
5. Sets & Lookups
6. Join & Grouping
7. Advanced Providers & Parallelism
8. Real-world Performance & Patterns
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details