Allow filtering for Include extension method
There is no ablity to filter included objects in ObjectQuery<TSource>.Include method.
Allow filter predicate in Include method in Entity Framework.
I suppose this method of ObjectQuery<TSource> may have following signature:
/// Includes related objects which meet to predicate
/// <typeparam name="TRelation">Type of related object on another end of navigation property</typeparam>
/// <param name="relationSelector">Expression that returns set of related objects</param>
/// <param name="predicate">Predicate that has to be met</param>
public System.Data.Objects.ObjectQuery<TSource> Include<TRelation>(Expression<Func<TSource, IEnumerable<TRelation>>> relationSelector, Expression<Func<TRelation, bool>> predicate);
Issue tracking this feature: https://github.com/aspnet/EntityFramework/issues/1833.
Dan Meierotto commented
I am not sure if you have see this, but this is possible now: http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx
See: Applying filters when explicitly loading related entities
OOO! i'd love to use Generics instead of strings anyday :) :) +1 +1 +1
Adrian Hesketh commented
How about having a generic include, e.g. .Include<Users>().Include<Roles>() instead of passing a string too.
Or something like Include<User>(u => u.Role) to include the role of a user instead of User.Include(Role).
@Matthieu - that's not acceptable. That's using a projection, which is not the same thing. We need to have predicates available to the Include .. so you can Filter, Take(x), Skip, etc...
By your suggestion/blog post, it's an anonymous projection with properties. That's doesn't mean we can get right poco's with their aggregations filled appropriately. Imaging asking (aka. Including) all the audit history for _each_ user retrieved? instead of just the most recent 10? or the the most recent 11-20 audit records for each user.. u get the drift.
Matthieu MEZIL commented
You can do it with EF4 even if it is less simple than the method you propose.
I wrote a post on it http://msmvps.com/blogs/matthieu/archive/2009/10/07/ef-include-with-where-clause.aspx