Method ZipLongest
| Edit this page View SourceZipLongest<T1, T2, TResult>(IEnumerable<T1>, IEnumerable<T2>, Func<T1?, T2?, TResult>)
Returns a projection of tuples, where each tuple contains the N-th element from each of the argument sequences. The resulting sequence will always be as long as the longest of input sequences where the default value of each of the shorter sequence element types is used for padding.
Declaration
public static IEnumerable<TResult> ZipLongest<T1, T2, TResult>(this IEnumerable<T1> first, IEnumerable<T2> second, Func<T1?, T2?, TResult> resultSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T1> | first | The first sequence of elements. |
IEnumerable<T2> | second | The second sequence of elements. |
Func<T1, T2, TResult> | resultSelector | A projection function that combines elements from all of the sequences. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | A sequence of elements returned by |
Type Parameters
Name | Description |
---|---|
T1 | The type of the elements of |
T2 | The type of the elements of |
TResult | The type of the elements of the result sequence. |
Remarks
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to use the ZipLongest
to merge three sequences.
var seq1 = new[] { "aaa", "bb", "c", "ddd", };
var seq2 = new[] { 1, 2, 3, 4, 5 };
// Zip two sequences together
var result = seq1
.ZipLongest(
seq2,
(a, b) => new { A = a, B = b, });
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [{ A = aaa, B = 1 }, { A = bb, B = 2 }, { A = c, B = 3 }, { A = ddd, B = 4 }, { A = , B = 5 }]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ZipLongest<T1, T2>(IEnumerable<T1>, IEnumerable<T2>)
Returns a projection of tuples, where each tuple contains the N-th element from each of the argument sequences. The resulting sequence will always be as long as the longest of input sequences where the default value of each of the shorter sequence element types is used for padding.
Declaration
public static IEnumerable<(T1?, T2?)> ZipLongest<T1, T2>(this IEnumerable<T1> first, IEnumerable<T2> second)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T1> | first | The first sequence of elements. |
IEnumerable<T2> | second | The second sequence of elements. |
Returns
Type | Description |
---|---|
IEnumerable<(T1, T2)> | A sequence of (T1, T2) containing corresponding elements from each of the sequences. |
Type Parameters
Name | Description |
---|---|
T1 | The type of the elements of |
T2 | The type of the elements of |
Remarks
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to use the ZipLongest
to merge two sequences.
var seq1 = new[] { "aaa", "bb", "c", "ddd", };
var seq2 = new[] { 1, 2, 3, 4, 5 };
// Zip two sequences together
var result = seq1.ZipLongest(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(aaa, 1), (bb, 2), (c, 3), (ddd, 4), (, 5)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Any of the input sequences is null. |
ZipLongest<T1, T2, T3, TResult>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, Func<T1?, T2?, T3?, TResult>)
Returns a projection of tuples, where each tuple contains the N-th element from each of the argument sequences. The resulting sequence will always be as long as the longest of input sequences where the default value of each of the shorter sequence element types is used for padding.
Declaration
public static IEnumerable<TResult> ZipLongest<T1, T2, T3, TResult>(this IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, Func<T1?, T2?, T3?, TResult> resultSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T1> | first | The first sequence of elements. |
IEnumerable<T2> | second | The second sequence of elements. |
IEnumerable<T3> | third | The third sequence of elements. |
Func<T1, T2, T3, TResult> | resultSelector | A projection function that combines elements from all of the sequences. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | A sequence of elements returned by |
Type Parameters
Name | Description |
---|---|
T1 | The type of the elements of |
T2 | The type of the elements of |
T3 | The type of the elements of |
TResult | The type of the elements of the result sequence. |
Remarks
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to use the ZipLongest
to merge four sequences.
var seq1 = new[] { "aaa", "bb", "c", };
var seq2 = new[] { 1, 2, 3, 4, };
var seq3 = new[] { 20, 5, 7, 12, 42 };
// Zip three sequences together
var result = seq1
.ZipLongest(
seq2,
seq3,
(a, b, c) => new { A = a, B = b, C = c, });
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [{ A = aaa, B = 1, C = 20 }, { A = bb, B = 2, C = 5 }, { A = c, B = 3, C = 7 }, { A = , B = 4, C = 12 }, { A = , B = 0, C = 42 }]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ZipLongest<T1, T2, T3>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>)
Returns a projection of tuples, where each tuple contains the N-th element from each of the argument sequences. The resulting sequence will always be as long as the longest of input sequences where the default value of each of the shorter sequence element types is used for padding.
Declaration
public static IEnumerable<(T1?, T2?, T3?)> ZipLongest<T1, T2, T3>(this IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T1> | first | The first sequence of elements. |
IEnumerable<T2> | second | The second sequence of elements. |
IEnumerable<T3> | third | The third sequence of elements. |
Returns
Type | Description |
---|---|
IEnumerable<(T1, T2, T3)> | A sequence of (T1, T2, T3) containing corresponding elements from each of the sequences. |
Type Parameters
Name | Description |
---|---|
T1 | The type of the elements of |
T2 | The type of the elements of |
T3 | The type of the elements of |
Remarks
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to use the ZipLongest
to merge two sequences.
var seq1 = new[] { "aaa", "bb", "c", };
var seq2 = new[] { 1, 2, 3, 4, };
var seq3 = new[] { 20, 5, 7, 12, 42 };
// Zip three sequences together
var result = seq1
.ZipLongest(
seq2,
seq3);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(aaa, 1, 20), (bb, 2, 5), (c, 3, 7), (, 4, 12), (, 0, 42)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Any of the input sequences is null. |
ZipLongest<T1, T2, T3, T4, TResult>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, IEnumerable<T4>, Func<T1?, T2?, T3?, T4?, TResult>)
Returns a projection of tuples, where each tuple contains the N-th element from each of the argument sequences. The resulting sequence will always be as long as the longest of input sequences where the default value of each of the shorter sequence element types is used for padding.
Declaration
public static IEnumerable<TResult> ZipLongest<T1, T2, T3, T4, TResult>(this IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, IEnumerable<T4> fourth, Func<T1?, T2?, T3?, T4?, TResult> resultSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T1> | first | The first sequence of elements. |
IEnumerable<T2> | second | The second sequence of elements. |
IEnumerable<T3> | third | The third sequence of elements. |
IEnumerable<T4> | fourth | The fourth sequence of elements. |
Func<T1, T2, T3, T4, TResult> | resultSelector | A projection function that combines elements from all of the sequences. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | A sequence of elements returned by |
Type Parameters
Name | Description |
---|---|
T1 | The type of the elements of |
T2 | The type of the elements of |
T3 | The type of the elements of |
T4 | The type of the elements of |
TResult | The type of the elements of the result sequence. |
Remarks
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to use the ZipLongest
to merge four sequences.
var seq1 = new[] { "aaa", "bb", "c", "ddd", };
var seq2 = new[] { 1, 2, 3, };
var seq3 = new[] { 20, 5, };
var seq4 = new[] { "zz", };
// Zip four sequences together
var result = seq1
.ZipLongest(
seq2,
seq3,
seq4,
(a, b, c, d) => new
{
A = a,
B = b,
C = c,
D = d,
});
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [{ A = aaa, B = 1, C = 20, D = zz }, { A = bb, B = 2, C = 5, D = }, { A = c, B = 3, C = 0, D = }, { A = ddd, B = 0, C = 0, D = }]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ZipLongest<T1, T2, T3, T4>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, IEnumerable<T4>)
Returns a projection of tuples, where each tuple contains the N-th element from each of the argument sequences. The resulting sequence will always be as long as the longest of input sequences where the default value of each of the shorter sequence element types is used for padding.
Declaration
public static IEnumerable<(T1?, T2?, T3?, T4?)> ZipLongest<T1, T2, T3, T4>(this IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, IEnumerable<T4> fourth)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T1> | first | The first sequence of elements. |
IEnumerable<T2> | second | The second sequence of elements. |
IEnumerable<T3> | third | The third sequence of elements. |
IEnumerable<T4> | fourth | The fourth sequence of elements. |
Returns
Type | Description |
---|---|
IEnumerable<(T1, T2, T3, T4)> | A sequence of (T1, T2, T3, T4) containing corresponding elements from each of the sequences. |
Type Parameters
Name | Description |
---|---|
T1 | The type of the elements of |
T2 | The type of the elements of |
T3 | The type of the elements of |
T4 | The type of the elements of |
Remarks
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to use the ZipLongest
to merge three sequences.
var seq1 = new[] { "aaa", "bb", "c", "ddd", };
var seq2 = new[] { 1, 2, 3, };
var seq3 = new[] { 20, 5, };
var seq4 = new[] { "zz", };
// Zip four sequences together
var result = seq1
.ZipLongest(
seq2,
seq3,
seq4);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(aaa, 1, 20, zz), (bb, 2, 5, ), (c, 3, 0, ), (ddd, 0, 0, )]
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Any of the input sequences is null. |