2

How can I load collection with include, I have tried these:

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

    But the filter is not supported here (Entities recovered not fulfill a condition).

  2. With LinQ To Entities :

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

But type do not contains it's associated Entities.

I can't write select new Type { type.Code, type.Entities } and I can't use anonymous type as it's not adapted for my repository layer.

Have you any ideas on how I can get a list of Type objects with its property Entities satisfying a condition?

Update

Thank you for your response

I think it's not a good idea to use the CTP , no ?

I am sorry, I did not explain my object model, I have :

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

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

And I want to use your second filtering proposal : Filter both entity sets

Do you have any suggestions without using the CTP ?

Thanks
Rad

Tim Post
  • 33,371
  • 15
  • 110
  • 174
rad
  • 21
  • 1
  • are you trying to include all related entities or do you want to apply filters as well when including? – Kris Ivanov Feb 21 '11 at 14:04
  • Hi Ivanov, I want apply filters as well when including – rad Feb 21 '11 at 14:20
  • Welcome to Stack Overflow. _Please_ simply edit your question (click the edit link) to add additional information, or use comments to reply to answers that you receive. The answer field is reserved strictly for answers. – Tim Post Feb 23 '11 at 14:17
  • Take a look at the answer to this question: http://stackoverflow.com/questions/4156949/ef4-linq-ordering-parent-and-all-child-collections-with-eager-loading-include/4157221#4157221 . Using this `CreateSourceQuery` as proposed there, it requires 2 roundtrips to the DB, but the CTP solution requires 2 trips as well. It seems to be impossible to apply filters to navigation properties when you are using `Include` to have only one trip to the DB. – Slauma Feb 23 '11 at 17:12

1 Answers1

0

as you stated you want to apply filters to the related entities, best to look at the new CTP5 of the EF4 http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx Look under "Applying filters when explicitly loading related entities"

Kris Ivanov
  • 10,476
  • 1
  • 24
  • 35