Method Cartesian
| Edit this page View SourceCartesian<T1, T2, TResult>(IEnumerable<T1>, IEnumerable<T2>, Func<T1, T2, TResult>)
Returns the Cartesian product of two sequences by enumerating all possible combinations of one item from each sequence, and applying a user-defined projection to the items in a given combination.
Declaration
public static IEnumerable<TResult> Cartesian<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
The method returns items in the same order as a nested foreach loop, but all sequences cached when iterated over. The cache is then re-used for any subsequent iterations.
This method uses deferred execution and stream its results.
Examples
The following code example demonstrates how to get the cartesian product of multiple sequences using Cartesian
.
var seq1 = new[] { 1, 2, 3, };
var seq2 = new[] { "foo", "bar", "quz", };
// Take a slice of the sequence
var result = seq1.Cartesian(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, foo), (1, bar), (1, quz), (2, foo), (2, bar), (2, quz), (3, foo), (3, bar), (3, quz)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Cartesian<T1, T2>(IEnumerable<T1>, IEnumerable<T2>)
Returns the Cartesian product of two sequences by enumerating all possible combinations of one item from each sequence.
Declaration
public static IEnumerable<(T1, T2)> Cartesian<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 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
The method returns items in the same order as a nested foreach loop, but all sequences are cached when iterated over. The cache is then re-used for any subsequent iterations.
This method uses deferred execution and stream its results.
Examples
The following code example demonstrates how to get the cartesian product of multiple sequences using Cartesian
.
var seq1 = new[] { 1, 2, 3, };
var seq2 = new[] { "foo", "bar", "quz", };
// Take a slice of the sequence
var result = seq1.Cartesian(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, foo), (1, bar), (1, quz), (2, foo), (2, bar), (2, quz), (3, foo), (3, bar), (3, quz)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Any of the input sequences is null. |
Cartesian<T1, T2, T3, TResult>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, Func<T1, T2, T3, TResult>)
Returns the Cartesian product of three sequences by enumerating all possible combinations of one item from each sequence, and applying a user-defined projection to the items in a given combination.
Declaration
public static IEnumerable<TResult> Cartesian<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
The method returns items in the same order as a nested foreach loop, but all sequences cached when iterated over. The cache is then re-used for any subsequent iterations.
This method uses deferred execution and stream its results.
Examples
The following code example demonstrates how to get the cartesian product of multiple sequences using Cartesian
.
var seq1 = new[] { 1, 2, 3, };
var seq2 = new[] { "foo", "bar", "quz", };
// Take a slice of the sequence
var result = seq1.Cartesian(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, foo), (1, bar), (1, quz), (2, foo), (2, bar), (2, quz), (3, foo), (3, bar), (3, quz)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Cartesian<T1, T2, T3>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>)
Returns the Cartesian product of three sequences by enumerating all possible combinations of one item from each sequence.
Declaration
public static IEnumerable<(T1, T2, T3)> Cartesian<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 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
The method returns items in the same order as a nested foreach loop, but all sequences are cached when iterated over. The cache is then re-used for any subsequent iterations.
This method uses deferred execution and stream its results.
Examples
The following code example demonstrates how to get the cartesian product of multiple sequences using Cartesian
.
var seq1 = new[] { 1, 2, 3, };
var seq2 = new[] { "foo", "bar", "quz", };
// Take a slice of the sequence
var result = seq1.Cartesian(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, foo), (1, bar), (1, quz), (2, foo), (2, bar), (2, quz), (3, foo), (3, bar), (3, quz)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Any of the input sequences is null. |
Cartesian<T1, T2, T3, T4, TResult>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, IEnumerable<T4>, Func<T1, T2, T3, T4, TResult>)
Returns the Cartesian product of four sequences by enumerating all possible combinations of one item from each sequence, and applying a user-defined projection to the items in a given combination.
Declaration
public static IEnumerable<TResult> Cartesian<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
The method returns items in the same order as a nested foreach loop, but all sequences cached when iterated over. The cache is then re-used for any subsequent iterations.
This method uses deferred execution and stream its results.
Examples
The following code example demonstrates how to get the cartesian product of multiple sequences using Cartesian
.
var seq1 = new[] { 1, 2, 3, };
var seq2 = new[] { "foo", "bar", "quz", };
// Take a slice of the sequence
var result = seq1.Cartesian(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, foo), (1, bar), (1, quz), (2, foo), (2, bar), (2, quz), (3, foo), (3, bar), (3, quz)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Cartesian<T1, T2, T3, T4>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, IEnumerable<T4>)
Returns the Cartesian product of four sequences by enumerating all possible combinations of one item from each sequence.
Declaration
public static IEnumerable<(T1, T2, T3, T4)> Cartesian<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 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
The method returns items in the same order as a nested foreach loop, but all sequences are cached when iterated over. The cache is then re-used for any subsequent iterations.
This method uses deferred execution and stream its results.
Examples
The following code example demonstrates how to get the cartesian product of multiple sequences using Cartesian
.
var seq1 = new[] { 1, 2, 3, };
var seq2 = new[] { "foo", "bar", "quz", };
// Take a slice of the sequence
var result = seq1.Cartesian(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, foo), (1, bar), (1, quz), (2, foo), (2, bar), (2, quz), (3, foo), (3, bar), (3, quz)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Any of the input sequences is null. |
Cartesian<T1, T2, T3, T4, T5, TResult>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, IEnumerable<T4>, IEnumerable<T5>, Func<T1, T2, T3, T4, T5, TResult>)
Returns the Cartesian product of five sequences by enumerating all possible combinations of one item from each sequence, and applying a user-defined projection to the items in a given combination.
Declaration
public static IEnumerable<TResult> Cartesian<T1, T2, T3, T4, T5, TResult>(this IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, IEnumerable<T4> fourth, IEnumerable<T5> fifth, Func<T1, T2, T3, T4, T5, 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. |
IEnumerable<T5> | fifth | The fifth sequence of elements. |
Func<T1, T2, T3, T4, T5, 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 |
T5 | The type of the elements of |
TResult | The type of the elements of the result sequence. |
Remarks
The method returns items in the same order as a nested foreach loop, but all sequences cached when iterated over. The cache is then re-used for any subsequent iterations.
This method uses deferred execution and stream its results.
Examples
The following code example demonstrates how to get the cartesian product of multiple sequences using Cartesian
.
var seq1 = new[] { 1, 2, 3, };
var seq2 = new[] { "foo", "bar", "quz", };
// Take a slice of the sequence
var result = seq1.Cartesian(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, foo), (1, bar), (1, quz), (2, foo), (2, bar), (2, quz), (3, foo), (3, bar), (3, quz)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Cartesian<T1, T2, T3, T4, T5>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, IEnumerable<T4>, IEnumerable<T5>)
Returns the Cartesian product of five sequences by enumerating all possible combinations of one item from each sequence.
Declaration
public static IEnumerable<(T1, T2, T3, T4, T5)> Cartesian<T1, T2, T3, T4, T5>(this IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, IEnumerable<T4> fourth, IEnumerable<T5> fifth)
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. |
IEnumerable<T5> | fifth | The fifth sequence of elements. |
Returns
Type | Description |
---|---|
IEnumerable<(T1, T2, T3, T4, T5)> | A sequence of (T1, T2, T3, T4, T5) containing 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 |
T5 | The type of the elements of |
Remarks
The method returns items in the same order as a nested foreach loop, but all sequences are cached when iterated over. The cache is then re-used for any subsequent iterations.
This method uses deferred execution and stream its results.
Examples
The following code example demonstrates how to get the cartesian product of multiple sequences using Cartesian
.
var seq1 = new[] { 1, 2, 3, };
var seq2 = new[] { "foo", "bar", "quz", };
// Take a slice of the sequence
var result = seq1.Cartesian(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, foo), (1, bar), (1, quz), (2, foo), (2, bar), (2, quz), (3, foo), (3, bar), (3, quz)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Any of the input sequences is null. |
Cartesian<T1, T2, T3, T4, T5, T6, TResult>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, IEnumerable<T4>, IEnumerable<T5>, IEnumerable<T6>, Func<T1, T2, T3, T4, T5, T6, TResult>)
Returns the Cartesian product of six sequences by enumerating all possible combinations of one item from each sequence, and applying a user-defined projection to the items in a given combination.
Declaration
public static IEnumerable<TResult> Cartesian<T1, T2, T3, T4, T5, T6, TResult>(this IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, IEnumerable<T4> fourth, IEnumerable<T5> fifth, IEnumerable<T6> sixth, Func<T1, T2, T3, T4, T5, T6, 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. |
IEnumerable<T5> | fifth | The fifth sequence of elements. |
IEnumerable<T6> | sixth | The sixth sequence of elements. |
Func<T1, T2, T3, T4, T5, T6, 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 |
T5 | The type of the elements of |
T6 | The type of the elements of |
TResult | The type of the elements of the result sequence. |
Remarks
The method returns items in the same order as a nested foreach loop, but all sequences cached when iterated over. The cache is then re-used for any subsequent iterations.
This method uses deferred execution and stream its results.
Examples
The following code example demonstrates how to get the cartesian product of multiple sequences using Cartesian
.
var seq1 = new[] { 1, 2, 3, };
var seq2 = new[] { "foo", "bar", "quz", };
// Take a slice of the sequence
var result = seq1.Cartesian(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, foo), (1, bar), (1, quz), (2, foo), (2, bar), (2, quz), (3, foo), (3, bar), (3, quz)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Cartesian<T1, T2, T3, T4, T5, T6>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, IEnumerable<T4>, IEnumerable<T5>, IEnumerable<T6>)
Returns the Cartesian product of six sequences by enumerating all possible combinations of one item from each sequence.
Declaration
public static IEnumerable<(T1, T2, T3, T4, T5, T6)> Cartesian<T1, T2, T3, T4, T5, T6>(this IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, IEnumerable<T4> fourth, IEnumerable<T5> fifth, IEnumerable<T6> sixth)
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. |
IEnumerable<T5> | fifth | The fifth sequence of elements. |
IEnumerable<T6> | sixth | The sixth sequence of elements. |
Returns
Type | Description |
---|---|
IEnumerable<(T1, T2, T3, T4, T5, T6)> | A sequence of (T1, T2, T3, T4, T5, T6) containing 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 |
T5 | The type of the elements of |
T6 | The type of the elements of |
Remarks
The method returns items in the same order as a nested foreach loop, but all sequences are cached when iterated over. The cache is then re-used for any subsequent iterations.
This method uses deferred execution and stream its results.
Examples
The following code example demonstrates how to get the cartesian product of multiple sequences using Cartesian
.
var seq1 = new[] { 1, 2, 3, };
var seq2 = new[] { "foo", "bar", "quz", };
// Take a slice of the sequence
var result = seq1.Cartesian(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, foo), (1, bar), (1, quz), (2, foo), (2, bar), (2, quz), (3, foo), (3, bar), (3, quz)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Any of the input sequences is null. |
Cartesian<T1, T2, T3, T4, T5, T6, T7, TResult>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, IEnumerable<T4>, IEnumerable<T5>, IEnumerable<T6>, IEnumerable<T7>, Func<T1, T2, T3, T4, T5, T6, T7, TResult>)
Returns the Cartesian product of seven sequences by enumerating all possible combinations of one item from each sequence, and applying a user-defined projection to the items in a given combination.
Declaration
public static IEnumerable<TResult> Cartesian<T1, T2, T3, T4, T5, T6, T7, TResult>(this IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, IEnumerable<T4> fourth, IEnumerable<T5> fifth, IEnumerable<T6> sixth, IEnumerable<T7> seventh, Func<T1, T2, T3, T4, T5, T6, T7, 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. |
IEnumerable<T5> | fifth | The fifth sequence of elements. |
IEnumerable<T6> | sixth | The sixth sequence of elements. |
IEnumerable<T7> | seventh | The seventh sequence of elements. |
Func<T1, T2, T3, T4, T5, T6, T7, 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 |
T5 | The type of the elements of |
T6 | The type of the elements of |
T7 | The type of the elements of |
TResult | The type of the elements of the result sequence. |
Remarks
The method returns items in the same order as a nested foreach loop, but all sequences cached when iterated over. The cache is then re-used for any subsequent iterations.
This method uses deferred execution and stream its results.
Examples
The following code example demonstrates how to get the cartesian product of multiple sequences using Cartesian
.
var seq1 = new[] { 1, 2, 3, };
var seq2 = new[] { "foo", "bar", "quz", };
// Take a slice of the sequence
var result = seq1.Cartesian(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, foo), (1, bar), (1, quz), (2, foo), (2, bar), (2, quz), (3, foo), (3, bar), (3, quz)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Cartesian<T1, T2, T3, T4, T5, T6, T7>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, IEnumerable<T4>, IEnumerable<T5>, IEnumerable<T6>, IEnumerable<T7>)
Returns the Cartesian product of seven sequences by enumerating all possible combinations of one item from each sequence.
Declaration
public static IEnumerable<(T1, T2, T3, T4, T5, T6, T7)> Cartesian<T1, T2, T3, T4, T5, T6, T7>(this IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, IEnumerable<T4> fourth, IEnumerable<T5> fifth, IEnumerable<T6> sixth, IEnumerable<T7> seventh)
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. |
IEnumerable<T5> | fifth | The fifth sequence of elements. |
IEnumerable<T6> | sixth | The sixth sequence of elements. |
IEnumerable<T7> | seventh | The seventh sequence of elements. |
Returns
Type | Description |
---|---|
IEnumerable<(T1, T2, T3, T4, T5, T6, T7)> | A sequence of (T1, T2, T3, T4, T5, T6, T7) containing 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 |
T5 | The type of the elements of |
T6 | The type of the elements of |
T7 | The type of the elements of |
Remarks
The method returns items in the same order as a nested foreach loop, but all sequences are cached when iterated over. The cache is then re-used for any subsequent iterations.
This method uses deferred execution and stream its results.
Examples
The following code example demonstrates how to get the cartesian product of multiple sequences using Cartesian
.
var seq1 = new[] { 1, 2, 3, };
var seq2 = new[] { "foo", "bar", "quz", };
// Take a slice of the sequence
var result = seq1.Cartesian(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, foo), (1, bar), (1, quz), (2, foo), (2, bar), (2, quz), (3, foo), (3, bar), (3, quz)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Any of the input sequences is null. |
Cartesian<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, IEnumerable<T4>, IEnumerable<T5>, IEnumerable<T6>, IEnumerable<T7>, IEnumerable<T8>, Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult>)
Returns the Cartesian product of eight sequences by enumerating all possible combinations of one item from each sequence, and applying a user-defined projection to the items in a given combination.
Declaration
public static IEnumerable<TResult> Cartesian<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(this IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, IEnumerable<T4> fourth, IEnumerable<T5> fifth, IEnumerable<T6> sixth, IEnumerable<T7> seventh, IEnumerable<T8> eighth, Func<T1, T2, T3, T4, T5, T6, T7, T8, 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. |
IEnumerable<T5> | fifth | The fifth sequence of elements. |
IEnumerable<T6> | sixth | The sixth sequence of elements. |
IEnumerable<T7> | seventh | The seventh sequence of elements. |
IEnumerable<T8> | eighth | The eighth sequence of elements. |
Func<T1, T2, T3, T4, T5, T6, T7, T8, 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 |
T5 | The type of the elements of |
T6 | The type of the elements of |
T7 | The type of the elements of |
T8 | The type of the elements of |
TResult | The type of the elements of the result sequence. |
Remarks
The method returns items in the same order as a nested foreach loop, but all sequences cached when iterated over. The cache is then re-used for any subsequent iterations.
This method uses deferred execution and stream its results.
Examples
The following code example demonstrates how to get the cartesian product of multiple sequences using Cartesian
.
var seq1 = new[] { 1, 2, 3, };
var seq2 = new[] { "foo", "bar", "quz", };
// Take a slice of the sequence
var result = seq1.Cartesian(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, foo), (1, bar), (1, quz), (2, foo), (2, bar), (2, quz), (3, foo), (3, bar), (3, quz)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Cartesian<T1, T2, T3, T4, T5, T6, T7, T8>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, IEnumerable<T4>, IEnumerable<T5>, IEnumerable<T6>, IEnumerable<T7>, IEnumerable<T8>)
Returns the Cartesian product of eight sequences by enumerating all possible combinations of one item from each sequence.
Declaration
public static IEnumerable<(T1, T2, T3, T4, T5, T6, T7, T8)> Cartesian<T1, T2, T3, T4, T5, T6, T7, T8>(this IEnumerable<T1> first, IEnumerable<T2> second, IEnumerable<T3> third, IEnumerable<T4> fourth, IEnumerable<T5> fifth, IEnumerable<T6> sixth, IEnumerable<T7> seventh, IEnumerable<T8> eighth)
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. |
IEnumerable<T5> | fifth | The fifth sequence of elements. |
IEnumerable<T6> | sixth | The sixth sequence of elements. |
IEnumerable<T7> | seventh | The seventh sequence of elements. |
IEnumerable<T8> | eighth | The eighth sequence of elements. |
Returns
Type | Description |
---|---|
IEnumerable<(T1, T2, T3, T4, T5, T6, T7, T8)> | A sequence of ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest> containing 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 |
T5 | The type of the elements of |
T6 | The type of the elements of |
T7 | The type of the elements of |
T8 | The type of the elements of |
Remarks
The method returns items in the same order as a nested foreach loop, but all sequences are cached when iterated over. The cache is then re-used for any subsequent iterations.
This method uses deferred execution and stream its results.
Examples
The following code example demonstrates how to get the cartesian product of multiple sequences using Cartesian
.
var seq1 = new[] { 1, 2, 3, };
var seq2 = new[] { "foo", "bar", "quz", };
// Take a slice of the sequence
var result = seq1.Cartesian(seq2);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, foo), (1, bar), (1, quz), (2, foo), (2, bar), (2, quz), (3, foo), (3, bar), (3, quz)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException | Any of the input sequences is null. |