3 IEnumerables into 1 tuple
c#, ienumerable, tuples
Solution
This is a scenario where it is easiest to use the iterator directly, rather than `foreach`:
using(var i1 = seq1.GetEnumerator())
using(var i2 = seq2.GetEnumerator())
using(var i3 = seq3.GetEnumerator())
{
while(i1.MoveNext() && i2.MoveNext() && i3.MoveNext())
{
var tuple = Tuple.Create(i1.Current, i2.Current, i3.Current);
// ...
}
}
The `// ...` here could be:
- `yield return tuple`
- `someList.Add(tuple);`
- or the actual thing you want to do
Problem
I've got 3 IEnumerables of integers and I would like to make an array of Tuple out of it. What's the best approach? If I had just 2 IEnumerables I would use Zip but in this case?