Mix Items to new List with Linq

Lets say this is my list {1, 2, 3, 4, 5, 6, 7, 8, 9 }

Now I would like to mix the items to following list: { 1, 9, 2, 8, 3, 7, ..}

Basically always one item from left side and one item from right side of the list.

Is there a possibilities to create this just by using linq statement?

Jon Skeet
people
quotationmark

Yes, you can do this using just LINQ (reasonably briefly, even), but it's not exactly pleasant:

  • Reverse the list
  • Zip it with the original list to get pairs (1, 9), (2, 8) etc
  • Flatten the result
  • Take only the original number

So:

var query = original.Zip(original.Reverse(), (x, y) => new[] { x, y })
                    .SelectMany(x => x)
                    .Take(original.Count());

If I were actually going to use this code, I'd definitely put a comment in there...

people

See more on this question at Stackoverflow