可选包括实体框架

时间:2014-06-06 18:46:26

标签: entity-framework linq include eager-loading

我正在经理一名经理。根据条件,必须强制执行某些包含才能获得预先加载。但有时我不想要所有数据,因此不应该使用包含。

这是我到目前为止所做的。

//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;

这不起作用。尽管includebooleans设置为true,但尚未加载包含。查询结果;

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]

但是如果我这样做很奇怪,所有包含都在工作

//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;

查看查询

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

为什么这种行为以及如何让它正常工作?

问候

1 个答案:

答案 0 :(得分:1)

我无法访问Visual Studio进行确认,但您可能会发现将Include调用的回复分配给它会对它进行排序 - 它会像QueryObject那样实现{ {1}}

IQueryable