C# Reference ยท LINQ
LINQ Zip() method
Reference for LINQ Zip() in C#: pair elements of two or three sequences, use a result selector, handle unequal lengths and pair neighbors.
Zip() walks two sequences in step and combines the elements at the same position, like the teeth of a zipper. The first result uses the first element of each sequence, the second result the second elements, and so on.
If the sequences have different lengths, Zip() stops when the shorter one ends. It is lazy and does not buffer. Use it to pair names with scores, compute differences between neighbors, or add an index to a sequence.
var pairs = names.Zip(scores); // tuples var lines = names.Zip(scores, (n, s) => $"{n}: {s}"); // custom result
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static IEnumerable<(TFirst First, TSecond Second)> Zip<TFirst, TSecond>( this IEnumerable<TFirst> first, IEnumerable<TSecond> second); public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>( this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector); public static IEnumerable<(TFirst First, TSecond Second, TThird Third)> Zip<TFirst, TSecond, TThird>( this IEnumerable<TFirst> first, IEnumerable<TSecond> second, IEnumerable<TThird> third);
| Name | Description |
|---|---|
first, second, third |
The sequences to combine, matched by position. |
resultSelector |
Combines one element from each sequence into the result. |
Returns: a lazy sequence with as many items as the shortest input. The tuple overloads give tuples with named items First, Second and Third.
Errors: ArgumentNullException if any argument is null. No error for unequal lengths.
Usage notes
- Versions: the selector overload has existed since .NET Framework 4.0. The tuple overload arrived in .NET Core 3.0 and the three-sequence overload in .NET 6.
- Unequal lengths are silent. Extra elements of the longer sequence are dropped, which can hide bugs; compare the counts first when both should match.
- Lazy and streaming: each side is enumerated as the result is read. A side-effecting source advances in lockstep, and nothing is read until you enumerate.
- Enumerating a source twice: zipping a sequence with
Skip(1)of itself enumerates it twice, so do not do it with a one-time sequence (for example a stream reader or a query with side effects); materialize withToList()first. - Parallel arrays to objects:
Zip()is a clean way to turn separate columns into records, but if you keep doing this, one list of objects is a better data structure. - Not a join.
Zip()matches by position. To match by key, useJoin(). - Index alternative:
Select((item, index) => ...)is simpler when you only need the position. - Deconstruction: tuples from
Zip()can be unpacked directly inforeach (var (a, b) in ...).
Related
Select(),Join(),Take()andSkip(),ToDictionary()- Lessons: LINQ operators, Tuples