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:

  1. It is enumerated by a foreach (C#) or For Each (Visual Basic) statement.
  2. It is enumerated by a collection operation such as ToArray, ToDictionary or ToList.
  3. LINQ operators such as First or Any are specified in the outermost part of the query.
  4. 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.


Related Articles