Skip to content
February 5, 2011 / pauldundon

Changes to Objects in LINQ Enumeration Lost

The Symptom: You have an IEnumerable(Of T) created with a LINQ query. You make changes to the objects in the enumeration, and then iterate across it again. The changes you have made are lost.

Possible Cause: Your LINQ query creates new objects, for example:


Dim q As IEnumerable(of OtherClass)=From x in someEnum Select New OtherClass(x)

The instances in the resulting enumeration will be created each time you iterate across it (ie, each time you use For Each o in q). So, each time you access the query you will get a fresh set of objects; changes made on previous iterations will be lost.

Possible Solution: Convert the enumeration to a list:


Dim q As IEnumerable(of OtherClass)=(From x in someEnum Select New OtherClass(x)).ToList()

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: