【问题标题】:Entity Framework - Navigating and Including properties through collections实体框架 - 通过集合导航和包含属性
【发布时间】:2010-09-23 16:59:15
【问题描述】:

我刚刚收到了大量的*blonde moment**,但它突出了我对 Entity Framework 的烦恼。我已经禁用了延迟加载,所以我强迫自己真正考虑我需要什么数据才能使应用程序尽可能快。

所以为了在查询中返回数据,我需要使用Include 方法:

var query = from item in context.Customers
                .Include(x=> x.Orders)
            select item

这很好,直到我想选择一个更深入层次结构的项目。即:

Customer 1-* Orders *-1 Factory 1-1 Factory Type

据我所知,急切返回所有这些数据的唯一方法是执行以下操作:

var query = from item in context.Customers
                .Include("Orders.Factory.FactoryType")
            select item

根据我的第一个示例,我无法使用上述System.Data.Entity Lambdas。有谁知道我是否在这里遗漏了一些明显的东西,或者我是否坚持通过集合为我的导航属性使用字符串声明?

如果我没有任何收藏,我可以写:

.Include(x=> x.Order.OrderType.Factory.FactoryType) // No bother

但由于Orders 集合,据我所知,没有办法进入子属性(FirstOrDefaultSingleOrDefault 等,不起作用)。


**这只是一个转折点,我恰好很喜欢金发女郎*

【问题讨论】:

    标签: linq-to-entities c#-4.0 entity-framework-4 code-first


    【解决方案1】:

    要包含 EntityCollections,请使用 Select 方法:

    var query = from item in context.Customers
               .Include(c => c.Orders.Select(o => o.Factory.FactoryType))
               select item
    

    请注意,这不是标准 ObjectQuery<T>.Include Method 的重载,而只是 ObjectQuery<T> 类的扩展方法,随 EF CTP4 提供。 p>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多