Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select on collection is not creating join. EF7.0.0-RC2-16583 #4271

Closed
jimmyrecardo opened this issue Jan 11, 2016 · 3 comments
Closed

Select on collection is not creating join. EF7.0.0-RC2-16583 #4271

jimmyrecardo opened this issue Jan 11, 2016 · 3 comments

Comments

@jimmyrecardo
Copy link

When I run this select:

var list = context.Employees.Select(x => new
{
    ID = x.ID,
    PhoneNumbers = x.PhoneNumbers,
});

This produces the following SQL queries:

SELECT [x].[ID]
FROM [Employee] AS [x]

Then this select is repeated for each employee row

SELECT [e].[ID], [e].[EmployeeID], [e].[PhoneNumber] 
FROM [EmployeePhoneNumber] AS [e]

I would expect that this should perform a SQL join whereas at the moment this pulling all records and doing a linq join.

The context/models are as follows:

//DB Context
public class AppDBContext: DbContext 
{ 
    public DbSet<Employee> Employees { get; set; }
    public DbSet<EmployeePhoneNumber> EmployeePhoneNumbers { get; set; }
}

//Models
public class Employee
{
    public long ID { get; set; }
    public virtual ICollection<EmployeePhoneNumber> PhoneNumbers{ get; set; }
}

public class EmployeePhoneNumber
{
    public long ID { get; set; }
    #region Employee
    public long EmployeeID { get; set; }
    public virtual Employee Employee { get; set; }
    #endregion Employee
    public string PhoneNumber {get;set;}
}
@Tarig0
Copy link

Tarig0 commented Jan 11, 2016

I suspect lazy loading is causing the issue, to get around it do an include of PhoneNumbers before the select

@jimmyrecardo
Copy link
Author

I did try the explicit include initially but this didn't appear to make a difference:

var list = context.Employees.Include(x => x.PhoneNumbers).Select(x => new
{
    ID = x.ID,
    PhoneNumbers = x.PhoneNumbers,
});

It does kinda work if i exclude the select, however this isn't the result i want

var list = context.Employees.Include(x => x.PhoneNumbers);

does a full select on all Employee fields and then this:

SELECT [e].[ID], [e].[EmployeeID], [e].[PhoneNumber],
FROM [EmployeePhoneNumber] AS [e]
INNER JOIN (
    SELECT DISTINCT [x].[ID]
    FROM [Employee] AS [x]
) AS [x] ON [e].[EmployeeID] = [x].[ID]
ORDER BY [x].[ID]

So it would seem that including it in the select is overriding the include?

@rowanmiller
Copy link
Contributor

Duplicate of #4007.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants