Well, you could do:
var pairs = sequence.Select((value, index) => new { value, index } ) .GroupBy(x => x.index / 2, x => x.value)
The result is IGrouping<int, T> with the key 0, 1, 2, etc., and the contents of each group are one or two elements.
However, I could write my own extension method:
public static IEnumerable<Tuple<T, T>> PairUp<T>(this IEnumerable<T> source) { using (var iterator = source.GetEnumerator()) { while (iterator.MoveNext()) { var first = iterator.Current; var second = iterator.MoveNext() ? iterator.Current : default(T); yield return Tuple.Create(first, second); } } }
This will give a sequence of tuples. The disadvantage here is that the final tuple will have a default value for T as the "second" element if the sequence has an odd number of elements. For reference types, where the sequence consists only of non-zero values, this is normal, but for some sequences this did not help.
Jon skeet
source share