I want to iterate through some list items in such a way that I would start from a specific position and then go left from it, after that go right from it.
In other words, something like this:
var items = new List<string>() { "Item1", "Item2", "Item3", "Item4", "Item5" };
string item = "Item3";
int index = items.IndexOf(item);
for (int i = index; i >= 0; i--)
yield return items[i];
for (int i = index + 1; i < items.Count; i++)
yield return items[i];
The result is: Item3, Item2, Item1, Item4, Item5
Is there a way to achieve this, but using only one for-loop?
Or is there some kind of conditional direction in LINQ?