entity-framework - 可选包含 Entity Framework

标签 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

为什么会出现这种行为以及如何使其正常工作?

问候

最佳答案

我无法访问 Visual Studio 进行确认,但您可能会发现将调用的返回值分配给 Include 会对其进行排序 - 它将类似于 QueryObject 实现了 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;

关于entity-framework - 可选包含 Entity Framework ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24088686/

相关文章:

c# - 为什么在实体中有私有(private)二传手

c# - 添加对象不起作用

php - 像 PHP 一样处理 HTML 文件

c# - MySQL 与 Visual Studio : Authentication Method 'mysql_clear_password' Not Supported, 如何修复?

c# - Linq 失败 - System.Data.Linq.dll 中发生类型 'System.Data.SqlClient.SqlException' 的第一次机会异常

c# - Linq 对象分组和求和

C# Linq Filter IEnumerable 动态属性和值

c++ - Xerces C++ SAX 解析问题 : expected class-name before '{' token

powershell - 与 Get-ChildItem cmdlet 的 -Include 参数混淆

c# - 带有 Power Tools 3.x Beta 的 EF5.x