you may be experiencing what is caused by the closure principle. you reference the
user property in your linq query. because the linq query in itself is executed as an (anonymous) method delegate, the closure principle applies.
quoting the above link:
in essence, a closure is a block of code which can be executed at a later time, but which maintains the environment in which it was first created - i.e. it can still use the local variables etc of the method which created it, even after that method has finished executing.
the usage of the
user property is subject to this principle. its value can have changed upon the execution of the linq query. to protect against this, the
user property should be copied to a local variable and that referenced in the linq query. like so:
var user = user; var users = msglist.select(m => m.from) .union(msglist.select(m => m.to)) .distinct() .where(u => u.id != user.id) .tolist();
when using a local reference copy to the
user property, another possibility for the
nullreferenceexception may lie with the select-union-distinct methods. when calling
where clause is executed on all items in the union of the two
select clauses. by default,
distinct executes the equals method from the
iquality interface, which would be called on the elements from
select(m => m.from) . if this element is null, it would cause the
- LINQ query sometimes (randomly, for the exact same data) throwing NullReferenceException
- Can I Use The Same Linq Code to Query for Different Conditions?
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- What is the return type for a anonymous linq query select? What is the best way to send this data back?
- Create linq query to search for contact the same way smartphone will do so
- Determine the source DataContext for a Linq to Sql query
- Using LINQ query result for data source for GridControl c#
- Can I use a LINQ IEnumerable result as the data source for a Gtk.TreeView?
- LINQ entity data model generated code error - The type 'DBContexts.Category' already contains a definition for 'ID'
- Reduce the line of code for this LINQ query
- Shortcut LINQ toentity query for the following code in MVC 3
- C# and ADO .NET entity data models with LINQ associations and entities for the same table
- How to write LINQ query for fetching the specific records and generating new result at the same time?
- JSON - LINQ Query for object or an array to group two data with same key value pair
- Can I assign the result of a Linq query to the source variable of the same query?
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- How to retrieve data for two different portions of a single web page using LINQ from the same database table?
- LINQ query code for complex merging of data
- LINQ Solution for the highest line with two or more same data
- Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
- How to get linq `ForEach` statement to return data on the method call being made for each list object?
- Performing part of a IQueryable query and deferring the rest to Linq for Objects
- LINQ to SQL: Complicated query with aggregate data for a report from multiple tables for an ordering system
- LINQ query needs either ascending or descending in the same query
- Using LINQ to get all property with the exact same value?
- LINQ Query to insert data into the database
- Linq query for only the first N rows for each unique ID
- What is the easiest way to save a LINQ query for later use?
- How does linq actually execute the code to retrieve data from the data source?
- linq - how do you do a query for items in one query source that are not in another one?
More Query from same tag
- ToList()-- does it create a new list?
- Pass Where and Orderby Clause using Func for Generic Method
- NHiberate 3 linq paged query + total count 1 hit to the database
- parsing by Linq to JSON
- MVC SaveChanges not saving and no error returned
- Comparing two counts in LINQ
- Dynamically Set Column Name In LINQ Query
- What do a & b stand for in .Aggregate( (a,b) => statement w/ a&b)
- linq query Take() takes precedence over Distinct()?
- Can I deduplicate a collection of objects based on several fields using a linq query?
- How to check if all list items have the same value and return it, or return an “otherValue” if they don’t?
- How can I return the contents of a collection inside a class using LINQ?
- Get List of Dicionary Value Property in c# using List of Keys
- Condittionaly enable/disable automatic formatting for children
- How to make nested list operations?
- LINQ Join with Multiple Conditions in On Clause
- Inconsistencies in data retrieval from csv file
- Update list items based on months in C#
- Entity Framework setup issue
- How to get the 5 highest values trough linq
- How to return single value from anonymous Linq.Expressions.Expression
- Number of occurrences of a character in a string
- LINQ get list from list by comparing enum
- How to get last 10 mins records using linq?
- Initializing strongly typed objects in LINQ to Entities
- Best way to extract rows from DataTable (based on date field) and copy to another DataTable
- LINQ: Create a Conditional Where Clause in child list
- EFCore 3: How to compare string columns on server side
- linq group by a start time to endtime on DateTime Column
- Problem with sorting objects