Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6470643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:08:05+00:00 2026-05-25T06:08:05+00:00

Hi, I have the following code which produces a strange behaviour. A property of

  • 0

Hi, I have the following code which produces a strange behaviour.
A property of an instance of objects contained in an IEnumerable produced by linq to Objects, does not get updated in subsequent foreach statements. The foreach statement should enuemerate the IEnumerable. Instead the solution is to enumerate it before.

Although I found the solution I have not seen this documented anywhere either in books or articles, dealing with similar examples. Perhaps somebody with intricate knowledge of linq can explain it.

It took me a day to pinpoint the exact cause of the error, and is not easy to debug in a large application. I then reproduced it in a much simpler environment, presented below.

public class MyClass
{
    public int val ;
 }

public class MyClassExtrax
{
    public MyClass v1 { get; set; }
    public int prop1 { get; set; }
}

void  Main() 
{
 List <MyClass> list1 = new List<MyClass>(); 
 MyClass obj1 = new MyClass(); obj1.val = 10; 
 list1.Add(obj1); 
 MyClass obj2 = new MyClass(); 
 obj2.val = 10; 
 list1.Add(obj2);

IEnumerable<MyClassExtrax> query1 =
     from v in list1
     where v.val >= 0
     select new MyClassExtrax{ v1=v ,  prop1=0 } ;

 //query1=query1.ToList(); solves the problem..but why is this needed..?
 foreach (MyClassExtrax fj in query1)
  {
    fj.v1.val = 40;
    fj.prop1 = 40;   //property does not get updated..
  }


 foreach (MyClass obj in list1)
  {
    Console.WriteLine("in list 1 value is {0} : ", obj.val);
  }


 foreach (MyClassExtrax obj in query1)
  {
   Console.WriteLine("in MyClassExtra list v1.val is {0}, prop1 is {1}  ", obj.v1.val,  obj.prop1); 
  }

 }

output:
in list 1 value is 40 :

in list 1 value is 40 :

in MyClassExtra list v1.val is 40, prop1 is 0

in MyClassExtra list v1.val is 40, prop1 is 0

As you can see prop1 does not get updated to 40.!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:08:05+00:00Added an answer on May 25, 2026 at 6:08 am

    This is quite simple, it’s well-documented. This is because of the deferred execution feature of LINQ. This code

    IEnumerable<MyClassExtrax> query1 =
     from v in list1
     where v.val >= 0
     select new MyClassExtrax{ v1=v ,  prop1=0 } ;
    

    doesn’t actually create the objects. It just creates an object that when iterated over actually creates the object. That is, you can think of query1 as the rule that knows how to spit out the objects when they are requested. So when you do this:

    foreach (MyClassExtrax fj in query1)
    {
        fj.v1.val = 40;
        fj.prop1 = 40;   //property does not get updated..
    }
    

    and then this:

    foreach (MyClassExtrax obj in query1)
    {
       Console.WriteLine("in MyClassExtra list v1.val is {0}, prop1 is {1}  ", obj.v1.val, obj.prop1); 
    }
    

    you are executing the rule for generating the objects twice. That is, two distinct sequences of objects are produced, and they are not shared between the sequences. That is why you don’t see the values updated; the references across the two iterations of the sequence are not the same.

    However, when you call ToList, and then walk the resulting list, now you have only produced one sequence of objects, and that sequence of objects is obviously the same across the two iterations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have code which produces the following LINQ expression (taken from the WhoCanHelpMe
I have some C# automation code which does interesting things with Microsoft.Office.Interop.Word.Document objects. The
I have the following code which produces a dictionary containing multiple lists; each list
I have the following code sample, which produces an error with the included constant
I have the following qtip code which when a link is clicked produces a
I have following code which is returning Foo @GET @Produces (MediaType.APPLICATION_XML) public Foo getXML
I have the following code which produces an OutOfMemory exception: byte[] buf = new
I have following code which works for radio buttons but need to be changed
I want to know is below code correct ? I have following code which
I have the following code which definitely returns a proper data result if I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.