Search for existing suggestions

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:

/// <summary>
/// Includes related objects which meet to predicate
/// </summary>
/// <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>
/// <returns>Query</returns>
public System.Data.Objects.ObjectQuery<TSource> Include<TRelation>(Expression<Func<TSource, IEnumerable<TRelation>>> relationSelector, Expression<Func<TRelation, bool>> predicate);

865 votes
Sign in
Sign in with: Facebook Google
Signed in as (Sign out)

We’ll send you updates on this idea

Mike Flasko shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →


Sign in
Sign in with: Facebook Google
Signed in as (Sign out)
  • Anonymous commented  ·   ·  Flag as inappropriate

    Part of the form fields have a file upload control and store the file on a local folder within the project using jQuery upload
    ○ Create another action method and a jQuery function which can be used for this (share the person ID in viewbag/tempdata after the save is finished)
    ○ It should allow uploading image files (.jpg, .png) only and once done we need to store the path of the file within database as a field
    ○ File path on the database should not include drive or full path, it should be relative to project path not absolute path
    ○ We need to display the relative path that was stored on table within the grid that we already have.

    =====full detaily don't write peaces and peaces okay

  • Chris Duff commented  ·   ·  Flag as inappropriate

    @Shannon - Thanks for pointing out this workaround. Unfortunately it is only useful in simple cases. It has the following problems:

    (1) It requires separate database calls which is bad for performance. Your example would result in two database calls rather than one. It's worse if it must be done N times. E.g. What if I want to query all active Blogs, loading their Posts that are Tagged with "entity-framework"? Something like (if filtered include were possible):

    var blogs = context.Blogs.Where(b => b.IsActive)
    .Include(b => b.Posts, p => p.Tags.Contains("entity-framework"))

    (2) It only works at the immediate child level. E.g. What if for a Blog I want to load only active Posts and for those Posts load any Tags that start with "e"? Something like (if filtered include were possible):

    var blog = context.Blogs.Where(b => b.Id == 1)
    .Include(b => b.Posts, p => p.IsActive)
    .Include(b => b.Posts.Select(p => p.Tags), t => t.StartsWith("e"))

    FYI the most promising workaround I've seen for more complex scenarios appears to be EntityFramework.Filters (available via NuGet). This might involve temporarily enabling/disabling global filters targeting 'included' entities. Also note that it currently has a bug stopping it from working on entities involved in inheritance.

  • Shannon Skinner commented  ·   ·  Flag as inappropriate

    While we wait for this feature to be implemented, there appears to be a work around for now:

    var blog = context.Blogs.Find(1);

    // Load the posts with the 'entity-framework' tag related to a given blog
    .Collection(b => b.Posts)
    .Where(p => p.Tags.Contains("entity-framework")

  • Patrick commented  ·   ·  Flag as inappropriate

    We should be able to specify what we want to include inside the include statement. This way, if we have a lot of information, we will not have a performance hit.

    For example:

    var blogs1 = context.Blogs
    .Include(b => b.Posts)

    This is fine, but imagine you have a LOT of posts. If you want to have only Posts from today, why not having something to filter it down directly from the SQL.
    var blogs1 = context.Blogs
    .Include(b => b.Posts, d=>d.Date>=DateTime.Now)

  • George Mohr commented  ·   ·  Flag as inappropriate

    This feature really couldn't be more important. I find myself building very inefficient queries and performing joins in memory due to the absence of this vital SQL function mapping. Please do add!!!

  • Erik Schierboom commented  ·   ·  Flag as inappropriate

    This is the feature I miss the most when using EF instead of NHibernate. It is so incredibly useful that I really hope it will be added soon.

  • Mike commented  ·   ·  Flag as inappropriate

    You may be able to workaround this by flipping around your query for Linq to Entities, then flipping it back with Linq to Objects. Using the example above:

    var q = context.Products.Where(p => p.UnitPrice > 20 & p.Category.StartsWith("a")).Include(p => p.Category);

    var r = q.ToList().Select(p => p.Category).Distinct();

    Note: This would not work if you are expecting Categories that don't have products in your results.

  • craig commented  ·   ·  Flag as inappropriate

    The suggestions below indicate that this can be with EF4. These comments are not correct, as the relationship depends specifically on change tracking. This can be illustrated as follows.

    var p = ParentTable.AsNoTracking().Select(o => new { Parent = o, Children = o.Children.Where(_ => _.Name.Length == "child")}).AsEnumerable().Select (_p => _p.Parent);

    does not work.

    var p = ParentTable.Select(o => new { Parent = o, Children = o.Children.Where(_ => _.Name.Length == "child")}).AsEnumerable().Select (_p => _p.Parent);

    Appears to work because a subset of the children happen to be change tracked.

  • Paul commented  ·   ·  Flag as inappropriate

    This is something we really need. I have looked at some the alternatives mentioned below and as other comments have explained they are not adequate solutions.

  • Dominik commented  ·   ·  Flag as inappropriate

    Hi again,

    I think maybe I don't expressed myself very well. What I really need is to get those paged Categories having only products with UnitPrice greater than 20.

  • Dominik commented  ·   ·  Flag as inappropriate

    I need to perform something like this:

    context.Categories.Where(ca = ca.StartsWith("a")).Include(ca => ca.Products.Where(p => p.UnitPrice > 20));

  • Piers commented  ·   ·  Flag as inappropriate

    Dan, the applying of filters mentioned in that post is when explicitly loading entities, not when Eagerly loading entities which is what Include would give.

    Moderator... this is the same suggestion as so the scores should be added together!

  • Piers commented  ·   ·  Flag as inappropriate

    Matthieu, your solution works if you know what you want to include at compile time... however the great thing about Include() is that it can be added to a query dynamically (e.g. within an if statement). Using a Projection means you lose that flexibility... doubly annoying as the introduction of a Projection also strips off any existing Include()s on the main query!!!

  • Jake commented  ·   ·  Flag as inappropriate

    Agreed, this doesn't solve the problem. This particular issue is a deal-breaker for our company and we'll have to switch back to LLBLGen Pro until this gets added to the framework.

← Previous 1

Feedback and Knowledge Base