如何使用include加载集合

时间:2011-02-21 13:53:44

标签: linq entity-framework include

如何使用include加载集合,我尝试过这些:

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

    但此处不支持过滤器(恢复的实体不符合条件)。

  2. 使用LinQ To Entities:

    var v = from type in ObjectContext.Type
            from entity in Type.Entities
            where entity.Parent == false
            select type;
    
  3. 但是类型不包含与之关联的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的情况下有任何建议?

    感谢
    弧度

1 个答案:

答案 0 :(得分:0)

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

下查看