【问题标题】:How can i load collection with include如何使用包含加载集合
【发布时间】:2011-02-21 13:53:44
【问题描述】:

如何使用包含加载集合,我已经尝试了这些:

  1. Type.Where(t => t.Entity.Where(e => e.Parent == false).Count() > 0).Include(e => e.Entity)

    但是这里不支持过滤器(恢复的实体不满足条件)。

  2. 使用 LinQ 到实体:

    var v = from type in ObjectContext.Type
            from entity in Type.Entities
            where entity.Parent == false
            select type;
    

但类型不包含它关联的Entities

我不能写 select new Type { type.Code, type.Entities } 也不能使用匿名类型,因为它不适合我的存储库层。

您对我如何获取Type 对象列表及其属性Entities 满足条件有任何想法吗?

更新

感谢您的回复

我认为使用 CTP 不是一个好主意,不是吗?

对不起,我没有解释我的对象模型,我有:

class Type {
  public string Code { get; set; }
  public IList<Entity> Entities { get; set; }
  ...
}

class Entity {
  public string Code { get; set; }
  ...
}

我想使用您的第二个过滤建议:过滤两个实体集

您有什么不使用 CTP 的建议吗?

谢谢
辐射

【问题讨论】:

  • 您是要包含所有相关实体,还是要在包含时也应用过滤器?
  • 嗨 Ivanov,我想在包含时也应用过滤器
  • 欢迎来到 Stack Overflow。 简单地编辑您的问题(单击编辑链接)以添加更多信息,或使用 cmets 回复您收到的答案。答案字段是专门为答案保留的。
  • 看看这个问题的答案:stackoverflow.com/questions/4156949/…。使用那里提出的CreateSourceQuery,它需要 2 次往返 DB,但 CTP 解决方案也需要 2 次往返。当您使用Include 只访问一次数据库时,似乎不可能将过滤器应用于导航属性。

标签: linq entity-framework include


【解决方案1】:

正如您所说,您希望将过滤器应用于相关实体,最好查看 EF4 http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx 的新 CTP5 查看“显式加载相关实体时应用过滤器”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多