LINQ Interview Question #132

What is Zip in LINQ?

Advanced LINQ, Practical Problems & Senior Interview Scenarios Senior Advanced

Quick Interview Answer

Zip combines elements from multiple sequences by position. The first element from one sequence is combined with the first from the other, then the second with the second, and so on.

Detailed Explanation

Zip combines elements from multiple sequences by position. The first element from one sequence is combined with the first from the other, then the second with the second, and so on.

Enumeration stops when the shortest participating sequence is exhausted. Zip is useful when position, rather than a matching key, defines the relationship.

Code Example

var names = new[] { "Ali", "Sara" };
var scores = new[] { 90, 85 };

var result = names.Zip(
    scores,
    (name, score) => new { name, score });