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
- C# Dynamic database filtering with Linq Expression
- Comparing times with LINQ to sql dynamically
- LINQ - Static columns transposition into aggregate rows values
- LINQ Select() or ForEach loop
- How to convert this into a dictionary of a dictionary
- How to avoid error if no datarows match the condition in linq query?
- How to check available hotel rooms
- how to group a field and check some condition in a faster way
- Remove Object From Hierarchical Collection
- C# Covariance issue
- C# - Linq to combine (or) join two datatables into one
- How to check the user already offer the ride using LINQ in NET Core
- Getting a cell from DataTable.Row.ItemArray with Linq
- LINQ/lambda: How can I query a DB table based on information from another table? (many to many relationship)
- Error when splitting and replacing chars C# email with LINQ Array Index not supported in LINQ
- select clause expression problems
- Changing an extention method from linq-to-sql to entity framework
- Linq query with one to many relation
- How can I get a SingleOrDefault value from a one to many join with LINQ?
- Linq2Sql: query optimisation
- Problem getting data with LINQ in C# from localdb
- How do I refactor a common LINQ subquery into a method?
- Linq using TakeWhile in a query
- object is not recognised in a Linq statement allthough in the same method the object is accessable
- Linq Like operator with constant length(can we use "_" ?)
- LINQ to SQL Conversion Overflows
- System.Linq and IEnumerable Group Help
- Testing for 'lazy loaded' null IEnumerable in c#?
- Parse string to C# lambda Func
- Expression.Call groupBy then Select?