Queryable.Zip Method

Definition

Overloads

Zip<TFirst,TSecond,TResult>(IQueryable<TFirst>, IEnumerable<TSecond>, Expression<Func<TFirst,TSecond,TResult>>)

Merges two sequences by using the specified predicate function.

Zip<TFirst,TSecond,TThird>(IQueryable<TFirst>, IEnumerable<TSecond>, IEnumerable<TThird>)

Produces a sequence of tuples with elements from the three specified sequences.

Zip<TFirst,TSecond>(IQueryable<TFirst>, IEnumerable<TSecond>)

Produces a sequence of tuples with elements from the two specified sequences.

Zip<TFirst,TSecond,TResult>(IQueryable<TFirst>, IEnumerable<TSecond>, Expression<Func<TFirst,TSecond,TResult>>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Merges two sequences by using the specified predicate function.

C#
public static System.Linq.IQueryable<TResult> Zip<TFirst,TSecond,TResult>(this System.Linq.IQueryable<TFirst> source1, System.Collections.Generic.IEnumerable<TSecond> source2, System.Linq.Expressions.Expression<Func<TFirst,TSecond,TResult>> resultSelector);

Type Parameters

TFirst

The type of the elements of the first input sequence.

TSecond

The type of the elements of the second input sequence.

TResult

The type of the elements of the result sequence.

Parameters

source1
IQueryable<TFirst>

The first sequence to merge.

source2
IEnumerable<TSecond>

The second sequence to merge.

resultSelector
Expression<Func<TFirst,TSecond,TResult>>

A function that specifies how to merge the elements from the two sequences.

Returns

IQueryable<TResult>

An IQueryable<T> that contains merged elements of two input sequences.

Exceptions

source1 or source2 is null.

Examples

The following code example demonstrates how to use the Zip method to merge two sequences.

C#
int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };

var numbersAndWords = numbers.AsQueryable().Zip(words, (first, second) => first + " " + second);

foreach (var item in numbersAndWords)
    Console.WriteLine(item);

// This code produces the following output:

// 1 one
// 2 two
// 3 three

Remarks

The Zip method generates a MethodCallExpression that represents calling Zip itself as a constructed generic method. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source1 parameter.

The method merges each element of the first sequence with an element that has the same index in the second sequence. If the sequences do not have the same number of elements, the method merges sequences until it reaches the end of one of them. For example, if one sequence has three elements and the other one has four, the resulting sequence will have only three elements.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Zip<TFirst,TSecond,TThird>(IQueryable<TFirst>, IEnumerable<TSecond>, IEnumerable<TThird>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Produces a sequence of tuples with elements from the three specified sequences.

C#
public static System.Linq.IQueryable<(TFirst First, TSecond Second, TThird Third)> Zip<TFirst,TSecond,TThird>(this System.Linq.IQueryable<TFirst> source1, System.Collections.Generic.IEnumerable<TSecond> source2, System.Collections.Generic.IEnumerable<TThird> source3);

Type Parameters

TFirst

The type of the elements of the first input sequence.

TSecond

The type of the elements of the second input sequence.

TThird

The type of the elements of the third input sequence.

Parameters

source1
IQueryable<TFirst>

The first sequence to merge.

source2
IEnumerable<TSecond>

The second sequence to merge.

source3
IEnumerable<TThird>

The third sequence to merge.

Returns

IQueryable<ValueTuple<TFirst,TSecond,TThird>>

A sequence of tuples with elements taken from the first, second and third sequences, in that order.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

Zip<TFirst,TSecond>(IQueryable<TFirst>, IEnumerable<TSecond>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Produces a sequence of tuples with elements from the two specified sequences.

C#
public static System.Linq.IQueryable<(TFirst First, TSecond Second)> Zip<TFirst,TSecond>(this System.Linq.IQueryable<TFirst> source1, System.Collections.Generic.IEnumerable<TSecond> source2);

Type Parameters

TFirst

The type of the elements of the first input sequence.

TSecond

The type of the elements of the second input sequence.

Parameters

source1
IQueryable<TFirst>

The first sequence to merge.

source2
IEnumerable<TSecond>

The second sequence to merge.

Returns

IQueryable<ValueTuple<TFirst,TSecond>>

A sequence of tuples with elements taken from the first and second sequences, in that order.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9