You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;}
}
The text was updated successfully, but these errors were encountered:
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?
When I run this select:
This produces the following SQL queries:
Then this select is repeated for each employee row
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:
The text was updated successfully, but these errors were encountered: