2

I'm working on a manager. Depending on the conditions some include must be enforced in order to get eager loading. But sometime I don't want all the data so the includes should not be applied.

This is what I've got so far.

//INFO : public partial class Entities : DbContext var Database = new Entities(); var result = Database.Department; if (includeHospitalEmployee) { result.Include(a => a.HospitalEmployee); } if (includeQuickScans) { result.Include(a => a.QuickScan); } return result; 

This doesn't work. The includes aren't loaded, although the includebooleans are set to true. Query results in;

SELECT [Extent1].[Code] AS [Code], [Extent1].[Discipline] AS [Discipline], [Extent1].[FinancialCode] AS [FinancialCode], [Extent1].[Name] AS [Name], [Extent1].[DepartmentManagerId] AS [DepartmentManagerId], [Extent1].[Show] AS [Show], [Extent1].[Id] AS [Id] FROM [dbo].[Department] AS [Extent1] 

But strange enough if i do this, all include are working

//INFO : public partial class Entities : DbContext var Database = new Entities(); var result = this.businessManagersFactory.Database.Department.Include(a => a.QuickScan);; if (includeHospitalEmployee) { result.Include(a => a.HospitalEmployee); } if (includeQuickScans) { result.Include(a => a.QuickScan); } return result; 

see the query

SELECT [Project1].[C1] AS [C1], [Project1].[Code] AS [Code], [Project1].[Discipline] AS [Discipline], [Project1].[FinancialCode] AS [FinancialCode], [Project1].[Name] AS [Name], [Project1].[DepartmentManagerId] AS [DepartmentManagerId], [Project1].[Show] AS [Show], [Project1].[Id] AS [Id], [Project1].[C2] AS [C2], [Project1].[Id1] AS [Id1], [Project1].[StartDateTime] AS [StartDateTime], [Project1].[EndDateTime] AS [EndDateTime], [Project1].[Shared] AS [Shared], [Project1].[ScanStatus] AS [ScanStatus], [Project1].[Title] AS [Title], [Project1].[Count] AS [Count], [Project1].[Comment] AS [Comment], [Project1].[HospitalEmployeeId] AS [HospitalEmployeeId], [Project1].[DepartmentId] AS [DepartmentId] FROM ( SELECT [Extent1].[Code] AS [Code], [Extent1].[Discipline] AS [Discipline], [Extent1].[FinancialCode] AS [FinancialCode], [Extent1].[Name] AS [Name], [Extent1].[DepartmentManagerId] AS [DepartmentManagerId], [Extent1].[Show] AS [Show], [Extent1].[Id] AS [Id], 1 AS [C1], [Extent2].[Id] AS [Id1], [Extent2].[StartDateTime] AS [StartDateTime], [Extent2].[EndDateTime] AS [EndDateTime], [Extent2].[Shared] AS [Shared], [Extent2].[ScanStatus] AS [ScanStatus], [Extent2].[Title] AS [Title], [Extent2].[Count] AS [Count], [Extent2].[Comment] AS [Comment], [Extent2].[HospitalEmployeeId] AS [HospitalEmployeeId], [Extent2].[DepartmentId] AS [DepartmentId], CASE WHEN ([Extent2].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2] FROM [dbo].[Department] AS [Extent1] LEFT OUTER JOIN [dbo].[QuickScan] AS [Extent2] ON [Extent1].[Code] = [Extent2].[DepartmentId] ) AS [Project1] ORDER BY [Project1].[Code] ASC, [Project1].[C2] ASC 

Why is this behaviour and how to get it working properly?

regards

1

1 Answer 1

2

I don't have access to Visual Studio to confirm but you will probably find that assigning the return from the call to Include will sort it - it will be something like a QueryObject that implements IQueryable

var Database = new Entities(); IQueryable<Department> result = Database.Departments; if (includeHospitalEmployee) { result = result.Include(a => a.HospitalEmployee); } if (includeQuickScans) { result = result.Include(a => a.QuickScan); } return result; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.