question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support filtered Include

See original GitHub issue

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<Rating>’ does not contain a definition for ‘User’ and no extension method ‘User’ accepting a first argument of type ‘IEnumerable<Rating>’ 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?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:519
  • Comments:199 (42 by maintainers)

github_iconTop GitHub Comments

160reactions
maumarcommented, Mar 24, 2020

Fixed in 21b9a35db594f7a383e855f922babbe54b3d38c5

Example:

customers.Include(c => c.Orders.Where(o => o.Name != "Foo").OrderByDescending(o => o.Id).Take(3))

Supported operations: Where, OrderBy/ThenBy, Skip, Take.

Only one filter allowed per navigation, so for cases where the same navigation needs to be included multiple times (e.g. multiple ThenInclude on the same navigation) apply the filter only once, or apply exactly the same filter for that navigation.

Example:

customers
    .Include(c => c.Orders.Where(o => o.Name != "Foo")).ThenInclude(o => o.OrderDetails)
    .Include(c => c.Orders).ThenInclude(o => o.Customer)

or

customers
    .Include(c => c.Orders.Where(o => o.Name != "Foo")).ThenInclude(o => o.OrderDetails)
    .Include(c => c.Orders.Where(o => o.Name != "Foo")).ThenInclude(o => o.Customer)

Feature will ship in the next preview, but should be available in our daily builds shortly for anyone interested in taking an early look. If you find some problems please file new issue as this thread is already very long.

102reactions
mikes-ghcommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Filtered Included - Learn Entity Framework Core 7
The Include method now supports filtering of the entities included. When applying Include to load related data, you can apply certain enumerable operations ......
Read more >
Filtering on Include in EF Core
Entity Framework core 5 is the first EF version to support filtered Include . How it works. Supported operations:.
Read more >
Eager Loading of Related Data - EF Core
Filtered include. When applying Include to load related data, you can add certain enumerable operations to the included collection navigation, ...
Read more >
Filtering Results Using Filtered Include Method in EF Core
In this article, we are going to learn about Filtered Include in Entity Framework Core, what operations it supports, and what are its...
Read more >
Filtered includes now supported in Entity Framework Core 5.0
Filtered includes is an awesome new feature in Entity Framework Core 5.0 which was released in November 2020.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found