It's happening because created
is a query, not a result. So, every time you enumerate it, you're evaluating the Select
over again from scratch.
If you want this to work, make created
an actual list, rather than just an IEnumerable
representing a query.
For example, add:
created = created.ToList();
You say:
I would expect the foreach-statement to execute the query, and trigger the creation of the objects
This is exactly what is happening. The problem is the creation of the objects happens every time you iterate over created
, not just the first time. Since the ElementAt()
method iterates over created
, it's just creating new A
s again.