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

  • Home
  • SEARCH
  • 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 324249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:06:23+00:00 2026-05-12T09:06:23+00:00

I encountered a behaviour I can´t quite understand when I was trying to populate

  • 0

I encountered a behaviour I can´t quite understand when I was trying to populate a GridView. Consider the following code:

DataTable table = GetStockMovement((int)ViewState[MOVEMENT_ID], false);
MovedProducts = from DataRow row in table.Rows
                     select new Product()
                     {
                         ProductId = (int)row["ProductId"],
                         ProductNo = row["ProductNo"].ToString(),
                         Name = row["ProductName"].ToString(),
                         Quantity = OriginalProducts.ToList().Find(p => p.ProductId == (int)row["ProductId"]).Quantity, 
                         QuantityDiff = (int)row["Change"]                                                                   
                     };

foreach (Product product in MovedProducts)
{
   Console.WriteLine(product.ProductId + ": " + product.Quantity); 
}
Console.WriteLine();
foreach (Product product in OriginalProducts)
{
    product.Quantity -= 1000;
}


foreach (Product product in MovedProducts)
{
   Console.WriteLine(product.ProductId + ": " + product.Quantity); 
}

The output of this was like this:

1: 10
2: 4

1: -990
2: -996

Product.Quantity is an int
MovedProducts and OriginalProducts are IEnumerables that are stored in ViewState.

Why does a product in MovedProducts change when I change in OriginalProducts? Are they not two totally different objects stored in different places in memory?

If I instead of the LINQ-expression used:

 List<FPRSProduct> completedProducts = new List<FPRSProduct>();
 foreach (DataRow row in table.Rows)
 {
     Product tmp = new Product();
     tmp.ProductId = (int)row["ProductId"];
     tmp.ProductNo = row["ProductNo"].ToString();
     tmp.Name = row["ProductName"].ToString();
     tmp.Quantity = OriginalProducts.ToList().Find(p => p.ProductId == (int)row["ProductId"]).Quantity;
     tmp.QuantityDiff = (int)row["Change"];                                                                   
     completedProducts.Add(tmp);
 }

Everything worked out as I expected it to, that is I get the same numbers before and after changing the Quantity of the OriginalProducts.

  • 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-12T09:06:24+00:00Added an answer on May 12, 2026 at 9:06 am

    LINQ utilizes what we call deferred execution. When you write out a linq query, you are not actually performing the tasks that query defines…you are simply defining it. The behavior defined by your query is executed at the moment of use…in your case, in the two foreach statements. The query is executed TWICE for EACH foreach, so even though data is copied…its copied from the source twice at two distinct points in time. The second foreach around your MovedProducts is retrieving the updated data in your OriginalProducts because execution of the query is deferred until the second foreach is actually executed.

    Note this KEY line of code in the ‘select new’ clause of your link query:

    Quantity = OriginalProducts.ToList().Find(p => p.ProductId == (int)row["ProductId"]).Quantity, 
    

    Since you update OriginalProducts between the first and second foreach, even though you are creating a distinc, new list of Product objects…since processing of the query is deferred, you execute the above line for each product twice. Once for the first foreach, and once again for the second foreach.

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

Sidebar

Related Questions

I'm making a bookmarklet, but I've encountered some wierd behaviour in IE8. The code
I'm trying to learn python and have encountered some strange behaviour. I am experimenting
I encountered the following ddl in a pl/sql script this morning: create index genuser.idx$$_0bdd0011
I encountered a problem when running some old code that was handed down to
I've encountered some very strange behaviour when debugging in VS 2010 with a WCF
I recently found a interesting behaviour of g++ when compared with MSVC++ 2008. Consider
I have encountered a Hibernate issue I just can't find my way around. We're
I'm trying to wrap my head around unit testing, and I've encountered a behavior
I'm quite new at WPF and recently encountered a problem. I got an ObservableCollection<
I have encountered odd behavior when using document.getElementById tonight. Duplicated in Firefox 3 and

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.