Production of dynamic collection with linq

advertisements

I wonder if linq already contains something to generate collections on the fly.

Lets say i want a dynamic collection of GUIDs, I currently use something like the following code for it:

public static IEnumerable<T> Generate<T>(Func<T> generator)
{
    for (;;)
    {
        yield return generator();
    }
}

var someIds = MyLinqExtensions.Generate(Guid.NewGuid).Take(10);

As such a construct is really handy sometimes, I'd rather not re-implement the wheel if such thing exists already.


You could use Range to generate a sequence of ints an then use Select to instantiate the item:

Enumerable.Range(1, 10).Select(i => Guid.NewGuid());