Repository navigation
Description
We keep this issue to track specifying filters inline when you specify eager loading in a query. However there are many scenarios that can be satisfied with global query filters:
Please learn about global query filters
We are seeing that a large portion of the scenarios customers want this feature for can already be better addressed using global query filters. Please try to understand that feature before you add your vote to this issue.
We are keeping this issue open only to represent the ability to specify filters on Include on a per-query basis, which we understand can be convenient on some cases.
Original issue:
Doing a .Where() inside a .Include() is allowed (by the syntax, it fails in execution). However, if you then follow that up with a .ThenInclude(), like so:
.Include(t => t.Ratings.Where(r => !r.IsDeleted))
.ThenInclude(r => r.User)You get the following error:
'IEnumerable' does not contain a definition for 'User' and no extension method 'User' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)
Is this something you're aware of and going to allow in a future version, or am I just doing something wrong and It's already supported?
Activity
popcatalin81 commentedon Mar 16, 2015
Historically this was not supported by EF, However this would be a nice feature to have. We had to implement something similar ourselves in the our code base using Linq tree rewriting. It hope the EF team considers this.
0xdeafcafe commentedon Mar 16, 2015
Yeah. I noticed discussion of this in the
.Include()design meeting docs. It looked as if it was road-mapped and going to be implemented. Although that could all just be a dreadful fantasy and I'm completely wrong.rowanmiller commentedon Mar 23, 2015
Moving to back log as a new feature (we do want to support filtered include but it is a lower priority than some other features that were available in EF6).
[-]Can't do a Where statement inside an Include, if it's followed by another Include[/-][+]Support filtered Include[/+]rowanmiller commentedon Mar 23, 2015
Opened new issue for better exception message #1883
mikes-gh commentedon Dec 12, 2015
Don't underestimate the value of filtered include. It was a top request in ef 6.
I came across this requirement on my first ef 7 project.
I'd take that over lazy loading any day.
rendmath commentedon Dec 30, 2015
Any non-trivial development using the Entity Framework will encounter this limitation.
It also has consequences on other technologies likes the OData protocol, because there is really no point to adding filtered $expands to the protocol if EF does not support them.
For those who are not (yet) familiar with the internals of EF Core, could you point us to the parts of the Entity Framework that would be impacted in order support this ?
joshmouch commentedon Jan 21, 2016
I too would love this feature.
gdoron commentedon Jul 25, 2016
@rowanmiller so what is the current way of using left join with a where clause \ on clause?
I currently query for the entire relationship and filter on the client.
Is there a better way of doing it?
It's a mess... and bad performance-wise.
lucacestola commentedon Jul 25, 2016
Will Filtered Loading be supported at configuration level, in order to have one to many navigation property filtered upstream? Can this feature be someway useful to avoid bad configurations with circular reference like this?
gdoron commentedon Jul 25, 2016
@lucacestola I'm not sure why you think it's related to conditional querying of a navigation property.
lucacestola commentedon Jul 25, 2016
@gdoron because it could be also applyed to one to many relationships and would be implicit.
Actually, with EF 6, I was not yet able to find a good solution for the relation type like the one at the posted link, without using a circular FK and, at the meantime, have the possibility to use an expression like this:
parent.MainChild.ChildProperty.I know that this kind of relations depends on a very custom logic so there is no simple way to address such a need, and I was hoping that, configuring the way relationships are loaded could almost partially address the issue.
Bartmax commentedon Jul 25, 2016
I just have the exactly same need as @gdoron (and not surprising, with
IsDeletedfields as well)is there any way to left join w/ where at db level?
203 remaining items
appsupport-gc commentedon Mar 8, 2021
I don't see why this isn't working then!
It gives me the error:
"Expression of type 'System.Linq.IOrderedQueryable
1[YBEdit.Shared.Models.EntitySection]' cannot be used for return type 'System.Linq.IOrderedEnumerable1[YBEdit.Shared.Models.EntitySection]'"Did this feature not make it beyond preview? I don't see anyone else suggesting using this method either.
Thanks,
Jonathan
cgountanis commentedon Mar 8, 2021
@appsupport-gc what if you remove the orderbys and maybe just try one where on an include and then the other can you narrow it down to anything?
appsupport-gc commentedon Mar 8, 2021
Oh I thought I saw suggested that orderbys on sub includes were now supported. I was hoping that was the case. It looks like I misread above and misunderstood the source linking here... Too bad. Some way to include OrderBys with ThenIncludes would be great! I guess my only option is manually stepping through each level of object? :(
Jonathan
appsupport-gc commentedon Mar 8, 2021
The example above suggests this should work:
"customers.Include(c => c.Orders.Where(o => o.Name != "Foo").OrderByDescending(o => o.Id).Take(3))"
That should be equivalent to dc.YearEntities.Include(ye => ye.EntitySections.OrderBy(es => es.SectionOrder)) which doesn't work for me.
mguinness commentedon Mar 8, 2021
Check the documentation for Filtered include and if it isn't working as documented then create a New issue with more information.
Mike-E-angelo commentedon Oct 15, 2021
Gosh what an awesome feature. Works like a champ. 💪 Thank you so much for putting this together and all your efforts out there. 👍
CollinAlpert commentedon Aug 10, 2022
This is really cool! How can I dynamically build a predicate for this? If I compile the
System.Linqexpression (since.Where()expects aFunc<T, bool>) I get the following exception:Expression of type 'System.Func`2[MyType,System.Boolean]' cannot be used for parameter of type 'System.Linq.Expressions.Expression`1[System.Func`2[MyType,System.Boolean]]'smitpatel commentedon Aug 10, 2022
@CollinAlpert
Queryable.Whereexpects aExpression<Func<T, bool>>parameter. The enumerable version expects one without Expression (just func). Your exception message say you are passing func rather than expression somewhere. The solution your question would depend on how you are constructing the expression tree. But that is not related filtered include or EF Core in any form. This kind of questions are more suitable for online question-answer forums like stackoverflow.CollinAlpert commentedon Aug 10, 2022
@smitpatel Thanks for your reply. The navigation property used in my
.Include()is anICollectionso I cannot pass anExpressioninto the.Wheremethod, since it is the enumerable's method, notQueryable. How do I make my navigation property use theQueryable.Wheremethod when filtering inside.Include()?itx-digital commentedon Aug 26, 2022
Hello,
Is there any chance you will implement this feature in EF Core 3 ?
Thank you.