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 6835293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:12:55+00:00 2026-05-26T23:12:55+00:00

I saw something weird, from the point that was not what I was expecting.

  • 0

I saw something weird, from the point that was not what I was expecting.

Let’s take this simple code to allow you to see what I did:

MyRepository db = new MyRepository();

// let's get them all
IQueryable<TblCals> cals = db.FindAllCalendars();

// now that we have everything, let's split up
List<TblCals> masters = cals.Where(x => x.IsMaster).ToList();
List<TblCals> childs =  cals.Where(x => x.Parent > 0).ToList();

// Let's change the name of the masters
foreach(var c in masters) c.Name = String.Concat("Master: ", c.Name);

// Let's send everything to the View
return View(new model() { masters, childs });

Now, the problem:

Why does the name changing in the List masters does also change the name of the objects that are in the childs?

When I do List<TblCals> masters = cals.Where(x => x.IsMaster).ToList(); shouldn’t this copy the elements instead of only referencing them from the cals object?

What can I do to prevent this behavior?

I could simple do:

MyRepository db = new MyRepository();

// let's get them all
IQueryable<TblCals> cals = db.FindAllCalendars();

// now that we have everything, let's split up
List<TblCals> masters = db.FindAllCalendarsThatAreMasters().ToList();
List<TblCals> childs =  db.FindAllCalendarsThatHaveParent().ToList();

but this will end up being 3 queries to the database, and I could simply get all first and then use that object… but not if the “master” object keeps changing when I change any sub query from it.


The major idea behind all this, is to see if it is safe to do:

public IEnumerable<JK_Users> ListAllUsers() {
    // Check / Add to memcache
    string key = "ListAllUsers";
    var users = MemcachedLayer.Get<IEnumerable<JK_Users>>(key);

    if(key == null)
        users = db.Tbl_Users.CachedQuery(key);

    return users;
}

public IEnumerable<JK_Users> FindUserById(decimal id) {
    // call ListAllUsers() instead Repository
    return this.ListAllUsers().FirstOrDefault(x => x.user_id == id);
}
public IEnumerable<JK_Users> FindUserByUsername(string username) {
    // call ListAllUsers() instead Repository
    return this.ListAllUsers().FirstOrDefault(x => x.username == username);
}

or I do have to call

public JK_Users FindUserById(decimal id) {
    return db.Tbl_Users.FirstOrDefault(x => x.user_id == id);
}
public JK_Users FindUserByUsername(string username) {
    return db.Tbl_Users.FirstOrDefault(x => x.username == username);
}

So I can use the Cache more efficiently.

  • 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-26T23:12:55+00:00Added an answer on May 26, 2026 at 11:12 pm

    No, collections only ever contain references for reference types. There’s nothing EF-specific here:

    List<StringBuilder> list1 = new List<StringBuilder>();
    List<StringBuilder> list2 = new List<StringBuilder>();
    StringBuilder builder = new StringBuilder();
    list1.Add(builder);
    list2.Add(builder);
    list1[0].Append("Foo");
    Console.WriteLine(list2[0]); // Still prints Foo...
    

    The entity framework is ensuring that for any one “entity”, there’s only a single object (within the context of your repository object). That means you can’t get into the state where you’ve changed the same field on two different objects representing the same entity (where entity is defined by its ID).

    This really is the logical approach – think of the values involved as entities; your two queries match the same entities for some results… why would you want those two results for the same entity to be independent objects? If someone refers to “Jon Skeet of Stack Overflow” and “Jon Skeet of Tilehurst, Reading, UK” that’s the same person – changing (say) the age via one result should change the age when seen via the other result.

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

Sidebar

Related Questions

I saw this in some code: $(<p/>).append(<div>something</div>).appendTo(body); and noticed that it automatically closed the
I just saw something like this in a C++ code (that compiles and probably
I hope this is something straightforward that I'm doing wrong. I saw something online
I saw a java function that looked something like this- public static<T> foo() {...}
I saw something like the following in our production code: select max(col_val) from table_name
I saw something that was a red flag for me when I ran this:
I was looking at some code and saw something like this: int d =
So I just saw a weird behavior In one script there is something like:
I saw something once for wrappers used to do this but it asked for
I was Dreamweaving a bit and saw something called Spry. Is this powerful or

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.