score:9

Accepted answer

i can see how that would be a bit confusing. here's the deal, succinctly:

  • the "receiver" of the query is treated as a value -- it never changes. if the value is a reference to a list, the value continues to be a reference to that list. the contents of the list might change, but which list does not change.

  • the local variables referred to by clauses of the query are treated as variables -- the latest values of those variables are always used.

let me give you an analogy to the real world. suppose you are in your kitchen. you have a drawer labeled "house" and a drawer labeled "name". in the drawer labeled "house" there is a piece of paper that says "1600 pennsylvania avenue". in the drawer labeled "name" there is a piece of paper that says "michelle". when you say:

var house = gethouse("1600 pennsylvania avenue");
var name = "michelle";
var query = from resident in house.residents 
            where resident.firstname == name 
            select resident;

that is like writing the query:

"list all the residents of the white house whose first name is (look at the piece of paper in the drawer marked "name" in my kitchen)"

the values returned by that query depend on (1) who is living in the white house, and (2) what name is written on the piece of paper when the query runs.

it is not like writing the query:

"list all the residents of (look at the piece of paper in the drawer marked "house" in my kitchen) whose first name is (look at the piece of paper in the drawer marked "name" in my kitchen)"

the object against which the query is running is not a variable. the contents of the white house can change. the name that the query is asking about can change. and therefore the results of the query can change in two ways -- with time, and with the value of the name variable. but what house the query is asking about does not ever change no matter what you do to the variable that held the reference. that variable is irrelevant to the query; its value was used to build the query.

score:1

it's as simple as that query (through an ienumerable) holds a reference to the collection it was created with.

changing another variable referencing the same list (ie setting list to null) does not change the reference query already holds.

  • the first test changes the underlying data in the actual list query references, which indeed changes the result.

  • in the second test you create a new list which leaves query still referencing the previous list. changing list does not change query.

  • the third test only nulls out list which has no effect on the reference query already holds.

score:3

changing where the reference 'list' points to does not change the original data. when the query expression was written the 'where' method took it's own reference to the data and will work on that data regardless of where the 'list' variable subsequently points.

this this case, where gets a new instance of an ienumerable which references the data that list currently points to, when you then change what list points to, the ienumerable does not change, it already has it's reference to the data.


Related Query

More Query from same tag