Zip

Combines two collections into a collection of pairs.

Parameters

Returns

IEnumerable<T> source

IEnumerable<T2> source2

IEnumerable<(T, T2)>

Usage

With same type and same length collections

//IEnumerable<int> source = { 1, 2, 3 }
//IEnumerable<int> source2 = { -1, -2, -3 }

IEnumerable<(int, int)> result = source.Zip(source2);

//result = { (1, -1), (2, -2), (3, -3)}

With different types collections

//IEnumerable<int> source = { 1, 2, 3 }
//IEnumerable<bool> source2 = { false, true, true }

IEnumerable<(int, bool)> result = source.Zip(source2);

//result = { (1, false), (2, true), (3, true)}

With different sized collections

//IEnumerable<int> source = { 1, 2, 3 }
//IEnumerable<string> source2 = { "One", "Two" }

IEnumerable<(int, string)> result = source.Zip(source2);

//result = { (1, "One"), (2, "Two")}

Last updated