score:9
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. changinglist
does not changequery
.the third test only nulls out
list
which has no effect on the referencequery
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.
Source: stackoverflow.com
Related Query
- LINQ query returns old results when source list is re-initialized
- When Including child elements LINQ query returns less results
- Linq query that returns obj results after comparing an input List against a List that is a property of an object
- How to get results from another query list if first linq query returns empty?
- Linq query to exclude from a List when a property value of List of different type are equal?
- Entity Framework Linq Query to List - Error when using contains: Only primitive types, enumeration types and entity types are supported
- Return Linq query results into List object
- LINQ query null exception when Where returns 0 rows
- Check if List is not null when using "contains" in LINQ query else select all records
- is it possible to use Take in LINQ when the query returns an anonymous type?
- Linq to query results of list of objects based on another list
- Linq sub query when using a repository pattern with EF code first
- How to return null when the Linq query returns empty value?
- Linq to Entities group query giving list of results in each group
- Linq query results to List collection
- LINQ query results not filtered when mocking IOrganizationService (Dynamics CRM)
- Linq to entities query that returns a count and a list of objects
- Linq Cannot be Null error is thrown when query returns no records
- Getting 'Data source is an invalid type' when binding Linq query to Gridview
- Linq query to return results when an ICollection is empty.
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Linq exception when join a list on a query result
- what does a linq query return when no results are found
- LINQ is able to filter on association, but returns 0 results when referenced directly
- Linq query results in the same references when initializing a variable
- NHibernate Linq with subquery returns 'Only one expression can be specified in the select list when the subquery is not introduced with EXISTS'
- Linq query where First() appears to return null when results are not found
- linq query returns too many results
- LINQ query to Data Table returns incorrect results
- How do I create a linq query that returns the distinct 3 letter prefix of words in a list using deferred execution
More Query from same tag
- Get items from list where index not equal to an int using LINQ
- compare 2 properties inside 2 linq lists and return a boolean based on the comparision
- Comparing 2 string arrays using Linq and Regex for a partial match
- Linq query with left join having multiple tables
- Expression.Invoke in Entity Framework?
- Split string on "\\" and/or "/" using LINQ?
- IsNull or Coalesce functionality in LINQ?
- Linq Should I Return List<T> Or IEnumerable<T> When I Still May Do More Later
- LINQ dynamic query
- LINQ - a part of string is contained in a list elements
- Check if a value is in the list with Linq c#
- Remove duplicate lists inside a list of lists in C#
- LINQ Query with grouping and ranking
- LINQ extension methods - Any() vs. Where() vs. Exists()
- How can I use Linq to extract this data from a List?
- Unable to create a constant value of type 'LookupObject'. Only primitive types or enumeration types are supported in this context
- How to return a 'Guid' from a 'Nullable<Guid>'?
- Linq with Optional Where clauses
- MVC: Best way to create Dynamic Group By
- How to have IN clause in linq to EF having two tables?
- C# Lambda Method syntax to obtain attribute values that match pattern in LINQ to XML
- How can I use a List<> as the condition for my where clause with Linq?
- Select from mulitle tables with count in Linq
- Anyone knows any Linq to XQuery implementation?
- .NET Convert the contents of a DataTable to another DataTable with a different schema
- Linq query - dynamic data types
- Filter items based on an IEnumerable<string>
- Create complex json from datatable c#
- How to make EF core translate custom sort into a plain 'When Then' instead of hierarchical
- Return input type of generic with type constraint in LINQ to Entities (EF4.1)