Entity Framework Core .Select() - SQL has all columns not the specified subset

c# entity-framework-core

Question

I am doing a simple query with a .Select() on a database view and was expecting to see a shorter list of columns in the generated SQL:

DbContext.LeadSearch.Select(ls => new
        {
            ls.Id,
            ls.BrokerName,
            ls.UpdateDate
        }).Take(2).ToList();

The generated SQL included all of the columns though:

SELECT TOP(@__p_0) [ls].[Id], [ls].[AssetNumber], [ls].[BrokerId],
[ls].[BrokerName], [ls].[FirstName], [ls].[IsApproved],
[ls].[LastName], [ls].[PurchaseAmount], [ls].[Yadda], [ls].[Yadda2], 
... [ls].[UpdateDate] FROM [dp].[vwLeadSearch] AS [ls]

From what I have read elsewhere I was expecting the SQL to have just the "Selected" columns.

LeadSearch is defined as a DbSet:

public DbSet<LeadSearch> LeadSearch { get; set; }

Mapped to the view:

modelBuilder.Entity<LeadSearch>().ToTable("vwLeadSearch").HasKey("Id");

This is with EF Core 2.1.1.

1
0
8/14/2018 8:01:19 PM

Popular Answer

One of the "columns" I included in the Select() was actually a calculated property on the class:

public string Name => FirstName + " " + LastName;

EF was smart enough to know it had to get all of the columns in order for this expression to complete. Changing my Select to get FirstName and LastName instead of Name fixed the issue.

1
8/16/2018 12:54:24 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow