I'm having a hard time finding the right LINQ syntax to use for the following iterator block:
class Program
{
class Operation
{
public IEnumerable<Operation> NextOperations { get; private set; }
}
class Item { }
static Item GetItem(Operation operation)
{
return new Item();
}
static IEnumerable<Item> GetItems(IEnumerable<Operation> operations)
{
foreach (var operation in operations)
{
yield return GetItem(operation);
foreach (var item in GetItems(operation.NextOperations)) // recursive
yield return item;
}
}
static void Main(string[] args)
{
var operations = new List<Operation>();
foreach (var item in GetItems(operations))
{
}
}
}
Maybe what I have is as good as it gets? For this particular code, yield return
inside an explicit foreach
is indeed the right solution?