实体框架 - 包括性能的多层次性 [英] Entity Framework - Include Multiple Levels of Properties

查看:144
本文介绍了实体框架 - 包括性能的多层次性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在include()方法的工作得很好的对象列表。但是,如果我需要去两层深?例如,下面的方法将返回与这里示出的包括属性ApplicationServers。然而,ApplicationsWithOverrideGroup是持有其它复杂对象另一个容器。我可以做一个包括()在该属性呢?或者我如何获得该财产完全加载?

目前的情况是,这个方法:

 公开的IEnumerable<&ApplicationServer的GT;得到所有()
{
    返回this.Database.ApplicationServers
        .INCLUDE(X => x.ApplicationsWithOverrideGroup)
        .INCLUDE(X => x.ApplicationWithGroupToForceInstallList)
        .INCLUDE(X => x.CustomVariableGroups)
        .ToList();
}

将填充只有Enabled属性(如下图),而不是应用程序或CustomVariableGroup属性(如下图)。我如何做到这一点?

 公共类ApplicationWithOverrideVariableGroup:EntityBase
{
    公共BOOL启用{搞定;组; }
    公共应用应用{搞定;组; }
    公共CustomVariableGroup CustomVariableGroup {搞定;组; }
}


解决方案

 使用System.Data.Entity的;query.Include(X => x.Collection.Select(Y => y.Property))

请参阅备注更多的例子。

请一定要添加使用System.Data.Entity的; 来获得的版本包含它接受拉姆达。

The Include() method works quite well for Lists on objects. But what if I need to go two levels deep? For example, the method below will return ApplicationServers with the included properties shown here. However, ApplicationsWithOverrideGroup is another container that holds other complex objects. Can I do an Include() on that property as well? Or how can I get that property to fully load?

As it stands now, this method:

public IEnumerable<ApplicationServer> GetAll()
{
    return this.Database.ApplicationServers
        .Include(x => x.ApplicationsWithOverrideGroup)                
        .Include(x => x.ApplicationWithGroupToForceInstallList)
        .Include(x => x.CustomVariableGroups)                
        .ToList();
}

Will populate only the Enabled property (below) and not the Application or CustomVariableGroup properties (below). How do I make this happen?

public class ApplicationWithOverrideVariableGroup : EntityBase
{
    public bool Enabled { get; set; }
    public Application Application { get; set; }
    public CustomVariableGroup CustomVariableGroup { get; set; }
}

解决方案

using System.Data.Entity;

query.Include(x => x.Collection.Select(y => y.Property))

See Remarks for more examples.

Make sure to add using System.Data.Entity; to get the version of Include that takes in a lambda.

这篇关于实体框架 - 包括性能的多层次性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆