Method ZipShortest
| Edit this page View SourceZipShortest<TFirst, TSecond, TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst, TSecond, TResult>)
Returns a projection of tuples, where each tuple contains the N-th element from each of the argument sequences. The resulting sequence is as short as the shortest input sequence.
Declaration
public static IEnumerable<TResult> ZipShortest<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TFirst> | first | The first sequence of elements. |
IEnumerable<TSecond> | second | The second sequence of elements. |
Func<TFirst, TSecond, 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 |
---|---|
TFirst | The type of the elements of |
TSecond | 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 ZipShortest
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
.ZipShortest(
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 }]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ZipShortest<TFirst, TSecond>(IEnumerable<TFirst>, IEnumerable<TSecond>)
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<(TFirst, TSecond)> ZipShortest<TFirst, TSecond>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TFirst> | first | The first sequence of elements. |
IEnumerable<TSecond> | second | The second sequence of elements. |
Returns
Type | Description |
---|---|
IEnumerable<(TFirst, TSecond)> | A sequence of (T1, T2) containing corresponding elements from each of the sequences. |
Type Parameters
Name | Description |
---|---|
TFirst | The type of the elements of |
TSecond | 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 ZipShortest
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.ZipShortest(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(aaa, 1), (bb, 2), (c, 3), (ddd, 4)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Any of the input sequences is null. |
ZipShortest<TFirst, TSecond, TThird, TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, IEnumerable<TThird>, Func<TFirst, TSecond, TThird, TResult>)
Returns a projection of tuples, where each tuple contains the N-th element from each of the argument sequences. The resulting sequence is as short as the shortest input sequence.
Declaration
public static IEnumerable<TResult> ZipShortest<TFirst, TSecond, TThird, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, IEnumerable<TThird> third, Func<TFirst, TSecond, TThird, TResult> resultSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TFirst> | first | The first sequence of elements. |
IEnumerable<TSecond> | second | The second sequence of elements. |
IEnumerable<TThird> | third | The third sequence of elements. |
Func<TFirst, TSecond, TThird, 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 |
---|---|
TFirst | The type of the elements of |
TSecond | The type of the elements of |
TThird | 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 ZipShortest
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
.ZipShortest(
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 }]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ZipShortest<TFirst, TSecond, TThird>(IEnumerable<TFirst>, IEnumerable<TSecond>, IEnumerable<TThird>)
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<(TFirst, TSecond, TThird)> ZipShortest<TFirst, TSecond, TThird>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, IEnumerable<TThird> third)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TFirst> | first | The first sequence of elements. |
IEnumerable<TSecond> | second | The second sequence of elements. |
IEnumerable<TThird> | third | The third sequence of elements. |
Returns
Type | Description |
---|---|
IEnumerable<(TFirst, TSecond, TThird)> | A sequence of (T1, T2, T3) containing corresponding elements from each of the sequences. |
Type Parameters
Name | Description |
---|---|
TFirst | The type of the elements of |
TSecond | The type of the elements of |
TThird | 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 ZipShortest
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
.ZipShortest(
seq2,
seq3);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(aaa, 1, 20), (bb, 2, 5), (c, 3, 7)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Any of the input sequences is null. |
ZipShortest<TFirst, TSecond, TThird, TFourth, TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, IEnumerable<TThird>, IEnumerable<TFourth>, Func<TFirst, TSecond, TThird, TFourth, TResult>)
Returns a projection of tuples, where each tuple contains the N-th element from each of the argument sequences. The resulting sequence is as short as the shortest input sequence.
Declaration
public static IEnumerable<TResult> ZipShortest<TFirst, TSecond, TThird, TFourth, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, IEnumerable<TThird> third, IEnumerable<TFourth> fourth, Func<TFirst, TSecond, TThird, TFourth, TResult> resultSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TFirst> | first | The first sequence of elements. |
IEnumerable<TSecond> | second | The second sequence of elements. |
IEnumerable<TThird> | third | The third sequence of elements. |
IEnumerable<TFourth> | fourth | The fourth sequence of elements. |
Func<TFirst, TSecond, TThird, TFourth, 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 |
---|---|
TFirst | The type of the elements of |
TSecond | The type of the elements of |
TThird | The type of the elements of |
TFourth | 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 ZipShortest
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
.ZipShortest(
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 }]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ZipShortest<TFirst, TSecond, TThird, TFourth>(IEnumerable<TFirst>, IEnumerable<TSecond>, IEnumerable<TThird>, IEnumerable<TFourth>)
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<(TFirst, TSecond, TThird, TFourth)> ZipShortest<TFirst, TSecond, TThird, TFourth>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, IEnumerable<TThird> third, IEnumerable<TFourth> fourth)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TFirst> | first | The first sequence of elements. |
IEnumerable<TSecond> | second | The second sequence of elements. |
IEnumerable<TThird> | third | The third sequence of elements. |
IEnumerable<TFourth> | fourth | The fourth sequence of elements. |
Returns
Type | Description |
---|---|
IEnumerable<(TFirst, TSecond, TThird, TFourth)> | A sequence of (T1, T2, T3, T4) containing corresponding elements from each of the sequences. |
Type Parameters
Name | Description |
---|---|
TFirst | The type of the elements of |
TSecond | The type of the elements of |
TThird | The type of the elements of |
TFourth | 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 ZipShortest
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
.ZipShortest(
seq2,
seq3,
seq4);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(aaa, 1, 20, zz)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Any of the input sequences is null. |