With Entity Framework, “Include” method is often used to load related entities / child collections. However, the method doesn’t let you use LINQ queryable methods like Where to filter entities to include which is a major drawback. Excluding soft deleted records or limiting the result is a frequent real life scenario which the “Include” method doesn’t support.
EF+ Query IncludeFilter lets you filter and perform LINQ queryable methods on related entities.
EF+ Query IncludeFilter
IncludeFilter method works the same as “Include” method but lets you use LINQ Queryable extension methods as part of the query to filter related entities.
Load one level
Load multiple levels
Real Life Scenarios
- Paging (Include a range of related entities)
- Security Access (Include available related entities)
- Soft Deleted Records (Include active related entities)
Behind the code
Step 1 - Custom Queryable
The first time IncludeFilter is called, a custom Queryable and Provider are created for your query and will be used to append all queries.
Step 2 - Iterate or Execute
When an immediate method is invoked to resolve the query, the initial query is modified to create a new query which includes related entities.
- The iterate method is invoked when it is possible to return multiple results like ToList() and ToArray()
- The execute method is invoked when it is possible to return only one result like First() and Last()
Entity Framework does already all the job to link related entities to the parent. No future logic is required.
- DO NOT work with AsNoTracking
- Entity Framework Core:
- Not supported yet.
- Entity Framework 5:
- Will never be supported. Use IncludeOptimized instead to filter collections
- Cannot be mixed with Include (Include doesn’t support projection)
- Cannot be mixed with IncludeOptimized
- Many to Many relation:
- Not supported yet
- Entities will contain all previously loaded related entities even if the Query does not return them. It’s a limitation due to how Entity Framework relation work.
- EF+ Query IncludeFilter: Full version or Standalone version
- Database Provider: All supported
- Entity Framework Version: EF6
- Minimum Framework Version: .NET Framework 4
Filtering included related entities is one of the most frequently asked questions in forums. EF+ Query IncludeFilter now make it possible without any learning curve required for a junior developer to understand what the method does.
Need help getting started? firstname.lastname@example.org
We welcome all comments, ideas and suggestions to improve our library.