.Net application development specialists
asp.net, c#, vb.net, html, javascript, jquery, html, xhtml, css, oop, design patterns, sql server, mvc and much more
contact: admin@paxium.co.uk

Paxium is the company owned by myself, Dave Amour and used for providing IT contract development services including


  • Application development - Desktop, Web, Services - with Classic ASP, Asp.net WebForms, Asp.net MVC, Asp.net Core, .NET 8/9/10
  • Azure - Azure Functions, App Services, Azure SQL, Service Bus, Blob Storage, Key Vault, API Management (APIM), Logic Apps and Application Insights
  • Html, Css, JavaScript, jQuery, React, C#, SQL Server, Ado.net, Entity Framework, NHibernate, TDD, WebApi, GIT, IIS
  • Database schema design, implementation & ETL activities
  • Website design and hosting including email hosting
  • Training - typically one to one sessions
  • Reverse Engineering and documentation of undocumented systems
  • Code Reviews
  • Performance Tuning
  • Located in Cannock, Staffordshire
Rugeley Chess Club Buying Butler Cuckooland Katmaid Pet Sitting Services Roland Garros 60 60 Golf cement Technical Conformity Goofy MaggieBears Vacc Track Find Your Smart Phone eBate Taylors Poultry Services Lafarge Rebates System Codemasters Grid Game eBate DOFF

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

  • Zip combines 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 Select for one list, Zip for two lists, and Join for key-based matching.