score:0
In the ResponseAddress part, you create a new instance of ResponseAddress class. This has no meaning for the relational database. When you turn your query results into list, then it's runtime's job to handle the union and it knows about objects whereas database server has no knowledge of ResponseAddress because that's not how it represents data.
score:0
DbSet and IDbSet implements IQueryable meaning a query is executed against the database only when:
- It is enumerated by a foreach (C#) or For Each (Visual Basic) statement.
- It is enumerated by a collection operation such as ToArray, ToDictionary or ToList.
- LINQ operators such as First or Any are specified in the outermost part of the query.
The following methods are called: the Load extension method on a DbSet, DbEntityEntry.Reload and Database.ExecuteSqlCommand
var users = db.Users.Select(x => new { Id = x.Id, Zip = x.Address.Zip });
As it is IQueryable above query has not yet executed against the database and users is an empty collection rather than null
var users = db.Users.Select(x => new
{
Id = x.Id,
Zip = x.Address.Zip
}).ToList();
Now calling the ToList() has executed the query to the db.
Source: stackoverflow.com
Related Articles
- LINQ to Entities null reference on Union with nested objects
- Null Object Reference on Retrieving Nested XML Elements with Linq / C#
- How to flatten nested objects with null leafs with linq expression
- How can I select one of two objects depending if one is null or not with a LinQ to Entities query.
- null output when flattening nested lists of objects with LINQ select many
- How to flatten nested objects with linq expression
- How do you construct a LINQ to Entities query to load child objects directly, instead of calling a Reference property or Load()
- Dynamic linq order by on nested property with null properties
- Error with Union in Linq to Entities
- null reference exception with linq .where clause
- C# Entity Framework with linq returns null reference
- Strange null reference exception in LINQ query with EF
- Create a nested list of items from objects with a parent reference
- LINQ union with optional null second parameter
- LINQ to Entities - Multiple Joins - Null Reference Exception on 'Select'
- sorting list of objects with null properties using linq
- Linq to Entities : how to handle null values in database with a contains
- EF Code First - Linq to Entities Union EqualityComparer
- Linq to Entities - insert all objects with TagID in string[] into an ICollection<Tag>
- LINQ query returning null for nested objects in Entity DB
- Writing a nested join with LINQ to Entities
- LINQ Order List By SubObject with possible Null Reference
- Creating class objects using LINQ with a highly nested XML
- Union two nested List<object> with LINQ
- Linq to entities query with nested query on the same table
- LINQ to Entities .contains is ignoring results with NULL
- Null reference exception in my LINQ to XML code
- query nested objects with LINQ
- LINQ:How to flatten deep nested objects with LINQ
- linq null reference error with orderby but firstordefault works
- Selecting several objects based on array of IDs
- Converting SQL to LINQ: The range variable conflicts with a previous declaration
- Searching a DataGridView for a match or partial match
- Linq query to get max values among lines in .csv file
- linq query from two database
- Extract list where string is contained inside a list inside the list
- What is the alternative for IN sql operator in LINQ for string field?
- Can we add new elements to a list using Parallel.ForEach()?
- Casting back query results stored in session for Custom Authorization
- Foreign Key Mapping
- How to avoid composing Where() operators in LINQ?
- Is there best practice to obtain elements/variables from collection based on different conditions
- LINQ - List Lowest Prices Only
- How does linq actually execute the code to retrieve data from the data source?
- LINQ groupby when two attributes are the same
- Zipping two lists in C#
- Change empty string to zero in list
- How to flatten a dictionary<string,List<string>> in linq and keep the key in the results
- How to use LINQ on a DataTable in Uipath
- LINQ to XML to with Multiple Namespaces in C#